Reputation: 97
A Summer Midterm question, still cannot wrap my head around it,
If someone can explain it to me I'll be glad
if print(8):
print(10000)
It prints 8 and i don't understand why?
Upvotes: 3
Views: 643
Reputation: 24107
if print(8):
print(10000)
Things the interpreter does:
print(8)
-> you see 8
in your terminal print()
always returns None
if None:
print(10000)
None
is a falsy value, it will not go inside the if-blockUpvotes: 5
Reputation: 477338
An if
statement will first evaluate the expression next to the if
keyword. So print(8)
. print(8)
will thus print 8
to the standard output channel, and return None
.
The if
statement will then evaluate the "truthiness" of that value. The truthiness of None
is False
. Thus means that the condition in the if
condition is not truthfull, and hence the body (print(10000)
) is not evaluated. It will thus print 8
, but not 10000
.
The documentation has a section Truth Value Testing [Python-doc]. As the documentation says:
- constants defined to be false:
None
andFalse
.
Upvotes: 3