In this tutorial, I will explain how to determine whether a number is even or odd in Python. As a data scientist at a financial firm in New York City, I recently faced a real-world problem where I needed to categorize a large dataset of transactions as even or odd dollar amounts. Python provides several easy ways to check the parity of a number.
Check if a Number is Even or Odd in Python
Python provides several ways to achieve this task. Let us learn some important methods.
Method 1. Use the Modulo Operator (%)
The most common approach to check if a number is even or odd in Python is using the modulo operator (%). The modulo operator returns the remainder after division. An even number is evenly divisible by 2 with no remainder, while an odd number leaves a remainder of 1 when divided by 2.
Here’s an example:
def is_even(number):
if number % 2 == 0:
return True
else:
return False
# Test the function
print(is_even(42))
print(is_even(31)) Output:
True
FalseI have executed the above example code and added the screenshot below.

In this code, the is_even function takes a number as input and uses the modulo operator to check if it is divisible by 2. If the remainder is 0, the function returns True indicating the number is even. Otherwise, it returns False for odd numbers.
Read Python Hello World Program
Method 2. Use Bitwise AND Operator (&)
Another efficient way to check for odd or even numbers in Python is by using the bitwise AND operator (&). This method relies on the fact that the least significant bit of an even number is always 0, while it’s 1 for odd numbers.
Here’s how it works:
def is_odd(number):
return number & 1
# Test the function
print(is_odd(27))
print(is_odd(48)) Output:
1
0I have executed the above example code and added the screenshot below.

The is_odd function performs a bitwise AND operation between the number and 1. If the result is 1, the number is odd. If the result is 0, the number is even.
Suppose you need to categorize a list of product IDs as even or odd. He can apply the is_odd function to each ID using a list comprehension:
product_ids = [105, 220, 317, 428, 531]
odd_ids = [id for id in product_ids if is_odd(id)]
print(odd_ids)Output:
[105, 317, 531]This code filters the product_ids list only to include the odd IDs.
Check out How to Check if a Variable Exists in Python?
Method 3. Use the bin() Function
The bin() function in Python converts a number to its binary representation as a string. In binary, even numbers always end with a 0 , and odd numbers end with a 1.
def is_even_bin(number):
binary_representation = bin(number)
if binary_representation[-1] == '0':
return True
else:
return False
# Test the function
print(is_even_bin(42))
print(is_even_bin(31))Output:
True
False I have executed the above example code and added the screenshot below.

The bin() function converts the number 42 to '0b101010' and 31 to '0b11111'.The last character ('0' for 42 and '1' for 31) determines if the number is even or odd.
Read How to Check if a Variable is a Byte String in Python?
Method 4. Use the isEven() Function
A custom isEven() function can be created that uses the modulo operator internally:
def isEven(number):
return number % 2 == 0
# Test the function
print(isEven(8500))
print(isEven(8501)) Output:
True
FalseThe isEven() function directly checks divisibility by 2 using the % operator. If the remainder is 0, it returns True (even), otherwise, False (odd).
Check out How to Check if both Variables are False in Python?
Example
Let’s combine these concepts into a practical example. Suppose you’re building a web application that offers a discount for tables with an even number of diners. You can use the following Python code to determine the discount eligibility:
def apply_discount(num_diners):
if num_diners % 2 == 0:
print(f"Table of {num_diners} diners qualifies for the even-group discount!")
else:
print(f"Table of {num_diners} diners does not qualify for the even-group discount.")
# Test the function
apply_discount(4)
apply_discount(5) Output:
Table of 4 diners qualifies for the even-group discount!
Table of 5 diners does not qualify for the even-group discount. The apply_discount function checks if the number of diners is even using the modulo operator and prints a message indicating whether the table qualifies for the discount.
Read How to Reverse a String in Python?
Conclusion
In this tutorial, we explored different methods to check if a number is even or odd in Python. By using the modulo operator, bitwise AND operator, bin() function, isEven() function you can easily determine if a number is even or odd in Python.
You may also like to read:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.