The modulus of 2 over 2 is zero:
>>> 2 % 2
0
So 2 % 2
produces 0
, which is a false value, and thus the if
statement doesn't match.
On the other hand, the modulus of 3 over to is one:
>>> 3 % 2
1
1
is a non-zero integer, so considered true.
In other words, the if i%2:
test matches odd numbers, not even. There are 3 odd numbers in your list.
Remember, modulus gives you the remainder of a division. 2 and 4 can be cleanly divided by 2, so there is no remainder. The if
test checks for a remainder.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…