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.
- The modulo operator %
- Choosing a message with if / else
- Why it also works for negative numbers
42 % 20 → even7 % 21 → oddThe 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.
number % 2🧦 Socks left over after making pairs== 0✅ Nothing left over: even"even" if ... else "odd"🏷️ Pick the right wordprint(f"{number} is {kind}")📣 Say the answerA whole number is even when dividing it by 2 leaves no remainder (number % 2 == 0) and odd when the remainder is 1.
Key points
number % 2gives the remainder after dividing by 2: 0 for 42."even" if number % 2 == 0 else "odd"chooses between two words in one line.kindthen holds the word, which the f-string puts into the sentence.- 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,% 5and so on. - Numbers that are not whole, such as 4.5: even and odd only apply to integers.
Example code
number = 42
# Even numbers leave no remainder when divided by 2.
kind = "even" if number % 2 == 0 else "odd"
print(f"{number} is {kind}")
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.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Using = instead of ==
A single = assigns a value; comparing needs ==.
if number % 2 = 0:
if number % 2 == 0:
Using / instead of %
/ gives the quotient, not the remainder.
number / 2 == 0
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.
- Odd, Even, or Neither Classify a number by whether it is an odd integer, an even integer, or neither. Easy
- Fizz Buzz Build a sequence using divisibility by 3 and 5. Easy
- Largest of Three Pick the largest of three numbers without a built-in max. Easy