Last modified: Nov 16, 2026
Python: Check If a Number Is Even or Odd
Checking whether a number is even or odd is one of the first tasks every Python beginner meets. It is simple, but it teaches a lot.
In this guide, you will learn several ways to solve it. You will also see how to avoid common mistakes.
What Makes a Number Even or Odd?
A number is even when it can be divided by 2 with no remainder. Examples are 0, 2, 4, 10, and 100.
A number is odd when dividing it by 2 leaves a remainder of 1. Examples are 1, 3, 7, 15, and 99.
This rule works for positive numbers, negative numbers, and zero. Zero is even because 0 divided by 2 has no remainder.
The Standard Way: The Modulo Operator
The % operator returns the remainder of a division. It is the most common tool for this task.
If number % 2 == 0, the number is even. Otherwise, it is odd.
number = 7
if number % 2 == 0:
print("Even")
else:
print("Odd")
Odd
The code is short and easy to read. It also works with negative numbers such as -4 and -9.
Wrap the Logic in a Function
Functions make your code reusable. They also keep the main part of your program clean.
Here is a function that returns a clear string result.
def check_even_odd(number):
# Return a readable result for any integer
if number % 2 == 0:
return "Even"
return "Odd"
print(check_even_odd(10)) # 10 is divisible by 2
print(check_even_odd(15)) # 15 leaves a remainder of 1
print(check_even_odd(-8)) # negative even number
print(check_even_odd(0)) # zero is even
Even
Odd
Even
Even
You can pass any value into the function. If you want to learn more about how parameters work, read this guide on Python function arguments explained.
Using a Boolean Result
Sometimes you do not need a string. A True or False value is often more useful.
The function below returns a boolean. You can use it directly inside an if statement.
def is_even(number):
# True when the remainder is zero
return number % 2 == 0
print(is_even(4))
print(is_even(9))
if is_even(20):
print("20 is even")
True
False
20 is even
Boolean helpers are handy in larger programs. They combine well with loops and conditions.
The Bitwise Alternative
There is a faster trick using the bitwise AND operator &. The last bit of an even number is always 0.
So number & 1 gives 0 for even numbers and 1 for odd numbers. This works because of how binary numbers are stored.
def is_even_bitwise(number):
# Check the last bit of the number
return (number & 1) == 0
print(is_even_bitwise(12))
print(is_even_bitwise(13))
True
False
The bitwise version is slightly faster. For most beginners, the modulo version is easier to read, so start with that.
Using the divmod Function
Python has a built-in function called divmod. It returns both the quotient and the remainder in one call.
You only need the remainder part to decide if a number is even or odd.
number = 42
quotient, remainder = divmod(number, 2)
if remainder == 0:
print(number, "is even")
else:
print(number, "is odd")
42 is even
This approach is useful when you need the quotient as well. Otherwise, the modulo operator is simpler.
Checking a List of Numbers
Real programs often deal with many numbers at once. A loop lets you check each one.
You can also collect even and odd numbers into separate lists.
numbers = [3, 8, 11, 20, 25, 30]
evens = []
odds = []
for n in numbers:
if n % 2 == 0:
evens.append(n)
else:
odds.append(n)
print("Even:", evens)
print("Odd:", odds)
Even: [8, 20, 30]
Odd: [3, 11, 25]
This pattern appears in data cleaning and reporting tasks. If you later need to compare collection types, see this article on Python set vs list key differences.
Using List Comprehension
You can write the same filter in one line. List comprehensions are short and readable.
numbers = range(1, 11)
evens = [n for n in numbers if n % 2 == 0]
odds = [n for n in numbers if n % 2 != 0]
print(evens)
print(odds)
[2, 4, 6, 8, 10]
[1, 3, 5, 7, 9]
This style is common in Python code. Just keep the condition simple so it stays easy to read.
Handling User Input
When you read input with input, you get a string. You must convert it to an integer with int.
Always validate the input. Users often type letters by mistake.
value = input("Enter a whole number: ")
try:
number = int(value)
if number % 2 == 0:
print(number, "is even")
else:
print(number, "is odd")
except ValueError:
print("That is not a valid integer")
Enter a whole number: 17
17 is odd
The try/except block prevents the program from crashing. This is a good habit for any input-based script.
Common Mistakes to Avoid
Many beginners compare a number to 0 with number % 2 = 0. That is invalid syntax. You must use == for comparison.
Another mistake is forgetting that input returns a string. Comparing a string with 0 will not work as expected.
Also remember that zero is even. Some tutorials skip this case, but the math is clear.
Where This Logic Shows Up
Even or odd checks appear in many classic exercises. They are often paired with number patterns.
For example, you might use them while exploring the Python Fibonacci sequence with examples. You can also combine the check with a Python prime number check program to test small numbers quickly.
The same idea powers simple games, alternating row colors, and basic data filters. It is a building block for bigger programs.
Quick Comparison of Methods
The modulo method is the best default choice. It is readable and works for every integer.
The bitwise method is faster but less obvious. Use it only when performance matters.
The divmod method is useful when you need both results. The boolean helper is great for reuse.
Pick one method and stay consistent. Clear code beats clever code most of the time.
Conclusion
Checking if a number is even or odd in Python is a small task with real value. The modulo operator gives you a clean and reliable answer.
You now know how to write a function, return a boolean, handle user input, and process lists. You also saw the bitwise shortcut.
Practice these examples with your own numbers. Then try changing them to count evens and odds in a range. That small step will make the logic stick.