An odd number is a number you cannot divide into two equal parts. An even number is a number you can evenly divide into two parts. In Python, you can use the modulo operator (%) to check if a number is odd or even.

For example:

n = 9 # 1. Check if a number is odd is_odd = n % 2 != 0 # 2. Check if a number is even is_even = n % 2 == 0

This guide teaches you how to check if a number is odd or even and how the modulo operator works in Python.

## The Modulo Operator in Python

In Python, the modulo operator (%) returns the remainder in the division between two numbers.

Here’s how you can use it:

a % b

If this operation returns 0, it means **a** is evenly divisible by **b**.

For example, if you have 15 slices of pizza for 3 guests and you want to check if you can share the 15 slices with your guests evenly, you can use the modulo operator:

print(15 % 3)

Output:

0

**Make sure to check my complete guide to modulos in Python.**

### How to Use Modulo to Figure Out If a Number Is Odd or Even?

The modulo operator returns the remainder in the division.

By definition:

**An odd number**isn’t evenly divisible by 2.**An even number**is evenly divisible by 2.

In the context of modulos, this means:

- Calculating modulo between an odd number and 2 returns 1 as a leftover value.
- Calculating modulo between an even number and 2 returns 0.

## How to Check If a Number Is Even in Python?

You can use the modulo operator in Python to check if a number is even.

**If you take a modulo between an even number and 2, the result is 0.**

To write a program that checks if a number is even:

**Take the modulo**between your target number and 2.**Use a comparison operator**to check if the result is equal to 0.

For example, let’s check if the number 9 is even:

number = 9 is_even = number % 2 == 0 print(is_even)

Output:

False

## How to Check If a Number is Odd in Python

You can also use the modulo operator in Python to check if a number is odd.

**If you take a modulo between an odd number and 2, the result is 1.**

To write a program that checks if a number is odd:

**Take the modulo**between your target number and 2.**Use a comparison operator**to check if the result is 1.

For instance, let’s see if the number 11 is odd:

number = 11 is_odd = number % 2 == 1 print(is_odd)

Output:

True

## Example 1. Write a Function That Checks If a Number Is Odd/Even

In the previous examples, you learned how to use the modulo operator to check if a value is odd or even in Python.

But the above examples are just standalone code expressions. If you want to reuse the odd/even logic, you’d have to rewrite the comparisons over and over again.

To improve your code quality and readability, you can implement functions for checking if an input number is odd/even. This way you can reuse the code by calling the name of the function.

Here are two functions for calculating if a value is odd or even:

def is_even(n): return n % 2 == 0 def is_odd(n): return n % 2 != 0

Now you can use these functions anywhere and any number of times in your code.

For example:

print(is_odd(10)) print(is_even(6))

Output:

False True

## Example 2. Check If User Input Is Odd/Even

A common beginner-friendly Python example is to take user input and check if the input is odd/even.

Here’s a program that does it for you:

number = int(input("Enter a number: ")) if number % 2 == 0: print("The number is even") else: print("The number is odd")

Example output:

Enter a number: 16 The number is even

Here:

- The
**input()**function takes the user input in the console. - The
**int()**call converts the input to an integer from a string (text). - The
**if-else**statement checks if the input number is divisible by 2 or not.

## Summary

Today you learned how to use Python to check if a number is odd or even.

To take home, you can use the modulo operator (%) to check for odd/even values. If the % operator returns 0 between a number and 2, the value is evenly divisible by 2 and is thus even. If the result is 1, the number is not divisible by 2, which means it’s odd.

Thanks for reading. Happy coding!

## Read Also

#### About the Author

- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.

##### Recent Posts

- Artificial Intelligence2023.03.14Best AI Tools of 2023 (Save Time & Boost Productivity)
- Python2023.02.16How to Pass a Variable from HTML Page to Another (w/ JavaScript)
- JavaScript2023.02.16JavaScript How to Get Selected Text from a Textbox
- JavaScript2023.02.15JavaScript Select All Text in Div with a Mouse Click