+ 1

# Hi! I'm a newbie here, just wanna ask about what does this sign "%" do in these codes, def even (x) if x % 2 == 0: ?

python 3 modulo

5 Answers

+ 15

Modulus division gives the remainder of division. Ex: 10 % 3 = 1.
3 evenly goes into 10 three times, and 1 is left over that 3 can't divide into.
What x % 2 == 0 is checking is if x is even. All even numbers divide into 2 evenly (so remainder would be 0).

+ 10

Things do not go as easily as you might think, though. The fun begins when negative numbers are concerned:
10 % 3 = 1
10 % -3 = -2
-10 % 3 = 2
-10 % -3 = -1
It always takes the divisor's sign.

+ 3

the sign % is called a modulus. It returns the remainder after divide 2 numbers. e.g. the above function checks if a number is even by determining whether the remainder will be zero after dividing the number by 2.

+ 1

simply.. checks if the "x " is divisible by 2 or not..
and if it is.. python executes the condition

+ 1

thanks!