Will the statement of “If 0:” be executed? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

14th Dec 2017, 1:17 PM
Claire
Claire - avatar
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.
14th Dec 2017, 1:28 PM
LunarCoffee
LunarCoffee - avatar
0
No because of the 0
25th Dec 2017, 10:00 PM
Michael Zhao
Michael Zhao - avatar
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..
30th Dec 2017, 10:19 AM
Pritam
Pritam - avatar