% Operator | SoloLearn: Learn to code for FREE!

+1

% Operator

evens=[i**2 for i in range(10) if i**2 % 2 == 0] print(evens) What is the % 2 == 0 saying in the above list comprehension. Please give me an actual numeric example.

4/23/2018 5:31:32 AM

Troy

5 Answers

New Answer

+11

% 2 is used a lot to check for even or odd. if 1, odd number, if 0 even number. Modulus returns the remainder so 1%2 = 1 2%2 = 0 3%2 = 1 4%2 = 0 5%2 = 1 6%2 = 0 7%2 = 1 8%2 = 0 etc. hopefully you see the pattern ;)

+7

% (Modulus) almost same with / (division). The difference is when 10 / 2 = 5 but when 10 % 2 = 0 Because it doesnt remains anything... i'll just give some example... 100 % 5 = 0 ✔ because it doesnt remains anything... 20 % 4 = 2 ✔ because its only remain 2... 7 % 4 = 3 ✔ because its only remain 3...

+1

It means only even numbers are accepted. All even numbers are divided by 2. If we divide a number by 2 and there is no remainder ,then it expressed by number % 2 = 0. Modulus sign ( % ) used to determine remainder. 4%2 = 0 16%2 = 0 5%2 =1 [ remainder 1]

+1

Thank you everyone for your help! It’s a lot clearer now!

-1