• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Even or Odd – Check if Number is Even or Odd Using % Operator

Python Even or Odd – Check if Number is Even or Odd Using % Operator

February 7, 2022 Leave a Comment

In Python, we can check if a number is even or odd very easily with the Python built in remainder operator %. If the remainder of a number after dividing by 2 is 0, then the number is even. If not, the number is odd.

def isEven(num):
    if (num % 2) == 0:
        return True
    else:
        return False

print(isEven(10))
print(isEven(15))

#Output:
True
False

When working with numbers in Python, it can be useful to know if the numbers you are working with are even or odd.

We can use the Python built in remainder operator % to get the remainder of a number after division.

To determine if a number is even using Python, we divide by 2. If the remainder after division is 0, then the number is even. If it is not 0, then the number is even.

Below is a function which will check if a number is even or odd in Python.

def isEven(num):
    if (num % 2) == 0:
        return True
    else:
        return False

print(isEven(10))
print(isEven(15))

#Output:
True
False

How to Check if a Number is Odd Using Python

To check if a number is odd using Python, we can use the Python remainder operator % in the same way as when we check if a number is even.

The difference is that the remainder for odd numbers after dividing by 2 is 1.

Below is a function which will check if a number is odd in Python.

def isOdd(num):
    if (num % 2) == 1:
        return True
    else:
        return False

print(isOdd(10))
print(isOdd(15))

#Output:
False
True

How to Determine if a Number is Divisible by Another Number

Using the Python remainder operator %, we can determine if a number is divisible by any other number.

For example, if we want to determine if a number is divisible by 3, we can use % with “3” in the following Python code.

def isDivisibleBy3(num):
    if (num % 3) == 0:
        return True
    else:
        return False

print(isDivisibleBy3(10))
print(isDivisibleBy3(15))

#Output:
False
True

If we want to check if a number is divisible by 5, just put 5 after the % operator.

def isDivisibleBy5(num):
    if (num % 5) == 0:
        return True
    else:
        return False

print(isDivisibleBy5(10))
print(isDivisibleBy5(15))

#Output:
True
True

Hopefully this article has been useful for you to understand how to determine if a number is even or odd in Python.

Other Articles You'll Also Like:

  • 1.  How to Add Commas to Numbers in Python
  • 2.  Length of Dictionary Python – Get Dictionary Length with len() Function
  • 3.  pandas to_pickle – Write DataFrame to Pickle File
  • 4.  pandas Drop Rows – Delete Rows from DataFrame with drop()
  • 5.  Convert String to Integer with int() in Python
  • 6.  Get Last Character in String in Python
  • 7.  Python Get Yesterday’s Date
  • 8.  Using Matplotlib and Seaborn to Create Pie Chart in Python
  • 9.  Python rjust Function – Right Justify String Variable
  • 10.  Python atanh – Find Hyperbolic Arctangent of Number Using math.atanh()

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy