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: ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

31st Mar 2017, 1:20 AM
Arvin Orpilla
Arvin Orpilla - avatar
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).
31st Mar 2017, 1:28 AM
Tamra
Tamra - avatar
+ 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.
31st Mar 2017, 6:49 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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.
31st Mar 2017, 1:34 AM
Brendan Rovholo
Brendan Rovholo - avatar
+ 1
simply.. checks if the "x " is divisible by 2 or not.. and if it is.. python executes the condition
31st Mar 2017, 4:59 PM
ashok pandey
ashok pandey - avatar
+ 1
thanks!
8th Apr 2017, 9:48 AM
Arvin Orpilla
Arvin Orpilla - avatar