PythonMastery

Check if a number is odd or even in Python

Read a whole number and print whether it is odd or even, using the remainder operator: n % 2 is 0 for even numbers.

A number is even when it divides by 2 with nothing left over. The % operator gives that remainder.

The program

python
n = int(input("Enter a whole number: "))

if n % 2 == 0:
    print(f"{n} is even")
else:
    print(f"{n} is odd")

Typed in this example: 7. Press Run to type your own.

output
Enter a whole number: 7
7 is odd

How it works

Checking a list of numbers

python
for n in [3, 8, 0, -5]:
    print(n, "even" if n % 2 == 0 else "odd")
output
3 odd
8 even
0 even
-5 odd

"even" if ... else "odd" is a conditional expression: a short form of if/else that gives back one of two values.

Learn the idea: Operators & Expressions, Control Flow: Conditionals