Categories: Python

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

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.

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.

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.

Data Science for beginners in this free online course Data Science is over complicated by…

2 months ago

15 Machine Learning Projects That Will Teach You All You Need as Machine Learning Authority…

2 months ago

Why learn Python? There are many reasons to learn Python, and that is the power…

2 months ago

There are a lot of Myths out there There are lot of Myths about being…

4 months ago

To be honest, I am not really a great programmer - that is not what…

4 months ago

What does it take to become a Data Scientist? Data Science is in a cross-field…

4 months ago