Last answered:

27 Jan 2023

Posted on:

27 Jan 2023

0

Please explain if x % 2 == 0:

if x % 2 == 0:
I have no idea on this line of code. Any idea?

1 answers ( 0 marked as helpful)
Instructor
Posted on:

27 Jan 2023

0

Hi Chong!
Thanks for reaching out!
if x % 2 == 0 means that if the remainder of a certain value divided by 2 would be zero, then the condition would be fulfilled. This means the same thing as saying 'if x is even' because values divided by 2 that give a remainder of 0 are 2, 4, 6, 8...
4/2 = 2
6/2 = 3
The remainder is the value that 'remains' when dividing. For instance
5/2 = 2 with a remainder of 1 because 2x2 = 4 and 4+1 gives 5.
Or 5 % 2 ==1
Hope this helps.
Best,
Ivan

Submit an answer