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?
if print(8):
print(10000)
Things the interpreter does:
print(8) -> you see 8 in your terminal print() always returns Noneif None:
print(10000)
None is a falsy value, it will not go inside the if-blockAn 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:
NoneandFalse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With