+ 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.
3 odpowiedzi
+ 6
% (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!