Python Program to Check Even or Odd Number
number % 2 == 0: a remainder of 0 means the number is even, anything else means it is odd.Even or odd is usually the first decision a beginner writes with if/else. The whole trick is the remainder operator %, which gives what is left over after a division. Every even number divides by 2 with nothing left over, and every odd number leaves exactly 1.
The same test shows up all over real code: alternating row colours in a table, splitting items between two workers, picking every second record, or deciding who plays first. Once you can write it from memory you will reuse it constantly, and it is a good warm-up before FizzBuzz and the prime number program.
| Input | One whole number (int), positive, negative or zero |
|---|---|
| Output | The text even or odd |
| Approach | Remainder of the number divided by 2 using % |
| Time complexity | O(1): one arithmetic operation |
Approach (step by step)
- Get the number and make sure it is an
int. - Calculate
number % 2. - If the result is
0, the number is even. - Otherwise the number is odd.
The % operator is explained in Python operators. The key fact is that for a positive divisor, Python always returns a result between 0 and the divisor minus one. With a divisor of 2 that leaves only two possible answers, 0 or 1, which is exactly the even/odd split.
How the code works
The condition n % 2 == 0 is a comparison, so it produces True or False, and if/else picks the matching branch. Note the two different symbols: % calculates the remainder, == compares two values.
Negative numbers work too. In Python the sign of % follows the divisor, so -3 % 2 is 1 and -3 is correctly reported as odd. Zero is even because 0 % 2 is 0.
for n in [10, 7, 0, -4, -3]:
print(n, n % 2)Other ways to test even and odd
You can replace the comparison with a bitwise test. The last binary digit of an odd number is always 1, so n & 1 is 1 for odd and 0 for even. It is correct for negative integers as well. For everyday code, n % 2 == 0 is clearer and just as fast to write, so prefer it unless you are already working with bits.
To split a whole list in one go, combine the test with a list comprehension: [n for n in numbers if n % 2 == 0] keeps only the even numbers.
| Technique | Even test | Good for |
|---|---|---|
| Modulo | n % 2 == 0 | Clear everyday code |
| Bitwise AND | n & 1 == 0 needs brackets: (n & 1) == 0 | Low-level or bit work |
| Truthiness | not n % 2 | Short conditions, less readable |
| Filtering a list | [n for n in nums if n % 2 == 0] | Whole collections |
Reading the number from the user
input() always returns text, so convert it with int() before using %. If the user types something that is not a whole number, int() raises a ValueError. A beginner program can ignore that, but a real one should catch it and ask again.
Examples you can run
Press Run, then edit the code and run it again.
Check a list of numbers
A function that returns the word keeps the logic reusable. The loop tests positive, negative and zero values.
def parity(n):
if n % 2 == 0:
return "even"
return "odd"
for n in [4, 9, 0, -7, 100]:
print(n, "is", parity(n))Expected output4 is even
9 is odd
0 is even
-7 is odd
100 is even
Alternative: bitwise AND and a list comprehension
n & 1 looks at the last bit. The comprehension then separates the even and odd order numbers into two lists.
orders = [101, 102, 103, 104, 105, 106]
evens = [n for n in orders if (n & 1) == 0]
odds = [n for n in orders if (n & 1) == 1]
print("even:", evens)
print("odd:", odds)Expected outputeven: [102, 104, 106]
odd: [101, 103, 105]
Interactive version with input()
The typed value is converted with int(). The stdin below stands in for what the user types.
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")Expected outputEnter a number: 27
27 is odd
Common mistakes
Using = instead of == in the condition
✗ Wrong
n = 8
if n % 2 = 0:
print("even")✓ Right
n = 8
if n % 2 == 0:
print("even")A single = assigns a value. Use == to compare.
Forgetting to convert the input to int
✗ Wrong
n = input("Number: ")
if n % 2 == 0:
print("even")✓ Right
n = int(input("Number: "))
if n % 2 == 0:
print("even")input() returns a string, and % on a string means text formatting. Wrap it in int() first.
Using the remainder as the test for even
✗ Wrong
n = 7
if n % 2:
print("even")
else:
print("odd")✓ Right
n = 7
if n % 2 == 0:
print("even")
else:
print("odd")A remainder of 0 means even. Compare it explicitly with == 0, or put the odd case in the if branch.
Python Program to Check Even or Odd Number: questions people ask
How do you check if a number is even or odd in Python?
Use the modulo operator: n % 2 == 0 means even, otherwise the number is odd. Put it in an if/else to print or return the result.
Is 0 even or odd in Python?
Zero is even. 0 % 2 equals 0, so the even test passes, which matches the mathematical definition.
Does the even/odd check work for negative numbers in Python?
Yes. Python's % takes the sign of the divisor, so -4 % 2 is 0 (even) and -3 % 2 is 1 (odd).
Keep learning
Step-by-step lessons:
Python if, elif and elsePython Operators
Related:
Python FizzBuzz ProgramPython Program to Check Prime NumberPython Program to Check Leap YearPython input() Function
More in number programs:
Python Program to Check Prime NumberPython Program to Find Factorial of a NumberPython Program to Print the Fibonacci SeriesPython Program to Check Armstrong NumberPython Program to Find GCD of Two NumbersPython Program to Check Leap YearPython Program to Find the Sum of Digits of a NumberPython Program to Print a Multiplication Table See all →