Python Odd or Even: Check if a Number Is Even or Odd

If you want to split numbers into two groups, such as seat numbers for two doors, check whether each one is odd or even in Python. The check itself is easy.

The bugs show up at the edges: input() hands you a string, and a float like 7.5 is neither.

Quick answer: if n % 2 == 0, the number’s even; otherwise it’s odd. Save this as quick.py and run python quick.py in a terminal:

seat = 17
print("even" if seat % 2 == 0 else "odd")   # odd

In this tutorial, I’ll show you the Python odd or even check as a program, as is_even() and is_odd() functions, on a list, and with its common errors. Outputs come from a real Python 3.12.5 run.

Odd or Even Program in Python with the % Operator

New to running Python files? You can follow these steps for every example:

  1. Save the code below as seat_input.py in a folder such as C:\pyguides (VS Code or Notepad works).
  2. Open Command Prompt in that folder and run python seat_input.py.
  3. Type a seat number and press Enter.

Save it as seat_input.py and run python seat_input.py in Command Prompt:

seat = int(input("Enter a seat number: "))

if seat % 2 == 0:
    print(f"Seat {seat} is even: house right.")
else:
    print(f"Seat {seat} is odd: house left.")

Output:

Enter a seat number: 27
Seat 27 is odd: house left.
Command Prompt output of a Python odd or even program that reads seat number 27 with input() and prints that it is odd
Typing 27 gives an odd, house-left seat.

% returns the remainder; even numbers leave 0. My post on the percent sign in Python covers the operator. You need int() because input() returns text; to reject letters, see checking if input is a number.

Write an is_even() Function for Odd-Even Checks

Python has no built-in iseven(), so you write two tiny functions. Save this as seat_func.py and run python seat_func.py in Command Prompt:

def is_even(n):
    return n % 2 == 0

def is_odd(n):
    return n % 2 != 0

for seat in [1, 2, 14, 27, 0]:
    print(f"{seat:>3}  is_even={is_even(seat)!s:<5}  "
          f"is_odd={is_odd(seat)}")

Output:

  1  is_even=False  is_odd=True
  2  is_even=True   is_odd=False
 14  is_even=True   is_odd=False
 27  is_even=False  is_odd=True
  0  is_even=True   is_odd=False
Command Prompt output of Python is_even() and is_odd() functions on seat numbers 1, 2, 14, 27 and 0
Seats 2, 14 and 0 are even; 1 and 27 are odd.

Zero is even, since 0 % 2 is 0. More on functions: how to define a function in Python.

Does Python’s Odd-Even Check Work for Negative Numbers?

Yes. Python’s % takes the divisor’s sign, so -3 % 2 is 1, not -1 as in JavaScript. Bitwise n & 1 reads the last bit instead.

Save this as seat_neg.py and run python seat_neg.py in Command Prompt:

for n in [7, -3, -4]:
    print(f"{n:>3}  n % 2 = {n % 2}  n & 1 = {n & 1}")

Output:

  7  n % 2 = 1  n & 1 = 1
 -3  n % 2 = 1  n & 1 = 1
 -4  n % 2 = 0  n & 1 = 0

Both columns agree: 7 and -3 give 1 (odd), and -4 gives 0 (even).

Separate Odd and Even Numbers in a List

The box office sends a list of sold seats, so you split it with two list comprehensions. Save this as seat_split.py and run python seat_split.py in Command Prompt:

sold = [101, 102, 103, 108, 111, 114, 117, 120]

left = [s for s in sold if s % 2 != 0]
right = [s for s in sold if s % 2 == 0]

print("Odd (left): ", left)
print("Even (right):", right)

Output:

Odd (left):  [101, 103, 111, 117]
Even (right): [102, 108, 114, 120]
Command Prompt output of Python list comprehensions splitting eight sold seats into odd house-left and even house-right lists
Four seats go house left (odd) and four go house right (even).

The pattern is explained step by step in my Python list comprehension tutorial, and filtering lists in Python shows the filter() version.

Python Odd or Even Errors with Strings and Floats

Forget int() and Python reads "27" % 2 as string formatting. Floats run, but 7.5 leaves 1.5.

Save this as seat_errors.py and run python seat_errors.py in Command Prompt:

for value in ["27", 7.5, 8.0]:
    try:
        print(f"{value!r:>5} % 2 = {value % 2!r}")
    except TypeError as err:
        print(f"{value!r:>5} -> TypeError: {err}")

print("int('27') % 2 =", int("27") % 2)

Output:

 '27' -> TypeError: not all arguments converted during string formatting
  7.5 % 2 = 1.5
  8.0 % 2 = 0.0
int('27') % 2 = 1
Command Prompt output showing a TypeError when a Python string is used with % 2, and remainders for 7.5 and 8.0
The string raises TypeError; int('27') fixes it.

Convert first, or use a Python integer check.

Next, try these beginner Python programs:

Python Odd or Even: Common Questions

How do you check if a number is even or odd in Python?

Use n % 2 == 0 for even and n % 2 != 0 for odd.

Is there an iseven() function in Python?

No. Write your own is_even(), or use arr % 2 == 0 on NumPy arrays and pandas columns.

How do I check if a number is odd in Python?

Use n % 2 != 0 or n & 1; both handle negatives.

Is 0 even or odd in Python?

Even. 0 % 2 returns 0.

Why does % 2 raise TypeError on input()?

input() returns a string, and % on a string means formatting. Use int().

Where is the % operator documented?