Beginner Python Exercises

Check if a Number Is Even or Odd in Python

One operator answers it: n % 2. The simplest example of the remainder operator at work.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • The modulo operator %
  • Choosing a message with if / else
  • Why it also works for negative numbers
n % 2
42 % 20 → even
7 % 21 → odd

The remainder after dividing by 2

The problem

Many decisions depend on whether a number is even: alternating row colors in a table, splitting players into two teams, checking a simple rule in a game. You need a quick, reliable test.

Think of it like this

Testing for even or odd is like pairing up socks. Put them in pairs of two: if none is left over, you had an even number; if one sock is left alone, the number was odd. The % operator tells you how many are left over.

From pairing socks to code
number % 2🧦 Socks left over after making pairs
== 0✅ Nothing left over: even
"even" if ... else "odd"🏷️ Pick the right word
print(f"{number} is {kind}")📣 Say the answer
Even numbers leave no remainder when divided by 2.
In one sentence

A whole number is even when dividing it by 2 leaves no remainder (number % 2 == 0) and odd when the remainder is 1.

Key points

  1. number % 2 gives the remainder after dividing by 2: 0 for 42.
  2. "even" if number % 2 == 0 else "odd" chooses between two words in one line.
  3. kind then holds the word, which the f-string puts into the sentence.
  4. Negative numbers work too, because Python's % always returns 0 or 1 here.

When should you use it?

  • Alternating behavior: striped table rows, taking turns, every second item.
  • Splitting items into two groups.
  • Simple validation rules in games and forms.

When not to use it

  • Testing divisibility by other numbers: the same idea works, but write % 3, % 5 and so on.
  • Numbers that are not whole, such as 4.5: even and odd only apply to integers.

Example code

Run code →
main.py
number = 42
# Even numbers leave no remainder when divided by 2.
kind = "even" if number % 2 == 0 else "odd"
print(f"{number} is {kind}")
Get a Run button for this example

Result of running the code

42 is even

Code explanation

number % 2 is the remainder after dividing by 2, so it is 0 for even numbers and 1 for odd ones. The conditional expression "even" if number % 2 == 0 else "odd" checks that remainder and stores the right word in kind. The f-string then builds the sentence 42 is even.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Using = instead of ==

A single = assigns a value; comparing needs ==.

Problem
if number % 2 = 0:
Fix
if number % 2 == 0:

Using / instead of %

/ gives the quotient, not the remainder.

Problem
number / 2 == 0
Fix
number % 2 == 0

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges