Home / Python / Example programs / Conditions

Python program: Even or odd

Check whether numbers are even or odd.

The code

for n in [7, 12, 0, -3]:
    if n % 2 == 0:
        print(n, "is even")
    else:
        print(n, "is odd")

Output

7 is odd
12 is even
0 is even
-3 is odd

How it works

% gives the remainder after division. An even number divided by 2 leaves no remainder, so n % 2 == 0 is the test.

Python features used

Learn the ideas behind it in the free lesson Making Decisions.