+ 4
Will the statement of âIf 0:â be executed?
Can anybody help explain this code - if 4%2: print(1) else: print(0) I thought 4%2 == 0, so the output should be 1. But the correct answer is 0. Do you know why?
3 Answers
+ 4
Because 4%2 = 0, it checks:
if 0:
which will always return false. It doesn't check:
if 4%2 == 0:
which would return true.
0
No because of the 0
0
(4%2) returns 0 to the 'if' statement which is equivalent to false so it executes the code under 'else' statement and prints 0.
It's very simple concept..