How to Check if a Number is Even or Odd with Python

What will you learn?

How to use the modulo operator to check if a number is even or odd with Python.

Step 1: Understand the modulo operator

Most do not know or understand the modulo operator when starting to program, and to be honest, many have been programming for years without discovering the power of the modulo operator.

The modulo operator is the percentage sign (%). What it calculates is the remainder, when dividing a number.

Say, you have 10 USD you need to divide between three people.

Then if you start by giving 1 dollar each, until there is not enough for another round.

The three people will have 3 USD each, and you will have 1 USD left. That 1 USD is the remainder.

Try in Python.

print(10 % 3)

It outputs 1.

Now you understand how modulo works.

Step 2: How to use module to see if number is even or odd

Now assume you have 11 USD and you you have two friends, one called Peter the other Natalie.

Then if you follow the above procedure of giving 1 USD each, until you don’t have enough for another round.

You will have 1 USD left, right?

If you had 122 USD and did the same, then you will have 0 USD left.

Try this experiment a few times and you will see, that for all Even numbers, you will end up with no money. But for all Odd numbers, you will end up with 1 USD at the end.

See, now we have made a way to see if a number is Even or Odd.

Step 3: Python program the can determine if number is Even or Odd

Now we put it all together.

number = int(input("Enter a number: "))
if number % 2 == 0:
    print("Even")
else:
    print("Odd")

Wow. That was amazing.

Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON

  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

Leave a Comment