Help with certain part in code

Hi I’m having a hard time understanding the line ( if number % 2 == 0:) in the example program:

numbers = [1, 1, 2, 3]
for number in numbers:
if number % 2 == 0:
continue
print(number)

What exactly does the %2 ==0 do?

Hi there,

So if you take n1%n2 as example

The modulo (%) allows you to see the remainder after you see how many times n2 fits into n1. (I’m sure there’s a better mathematical term for this)

Example:

10%3 == 1 (evaluates to True) because 3 x 3=9, and then the difference with 10 is 1.
10%2 == 0 (also evaluates to True) because 5 x 2=10 and the difference with 10 is 0

The latter is often used to check for even numbers.

1 Like

Seems like Wikipedia is happy with the term which is probably more useful for most people anyway :slightly_smiling_face:- Modulo operation - Wikipedia

1 Like