While working with numbers you might have found the need to use the Python Modulo operator in your program. Let’s find out more about it.

**The Python Modulo operator returns the remainder of the division between two numbers and it is represented using the % symbol. The Modulo operator is part of Python arithmetic operators. Here is an example of how to use it: 5 % 2 is equal to 1 (the remainder of the division between 5 and 2).**

Let’s go through some examples to explain the meaning of modulo in Python.

## What is the Python Modulo Operator?

One of the arithmetic operators available in Python is the Modulo Operator that returns the remainder of the division between two numbers.

To represent the modulo operator we use the symbol % between the two operands.

Here are some examples of how to use it:

```
>>> 5 % 2
1
>>> 8 % 3
2
>>> 8 % 2
0
```

Let’s have a look at the examples above:

- 5 modulo 2 is 1 because 5 divided by 2 is 2 with a remainder of 1.
- 8 modulo 3 is 2 because 8 divided by 3 is 2 with a remainder of 2.
- 8 modulo 2 is 0 because 8 divided by 2 is 4 with a remainder of 0.

You might see that the modulo operator is sometimes referred to as modulus or Python modulo division.

## Python Modulo of Negative Integers

In the previous section, we have seen how the modulo works for positive integers.

Let’s test it on negative integers:

```
>>> -5 % 2
1
>>> -5 % -2
-1
>>> 5 % -2
-1
```

As you can see the Python module returns a remainder of the same sign as the divisor.

## Python Modulo of a Float

Now, we will see how the modulo works with floating-point numbers.

```
>>> 3.4 % 2
1.4
>>> 3.4 % 2.0
1.4
>>> 3.4 % 2.2
1.1999999999999997
```

The first two examples work in the same way we have already seen with integers.

Let’s confirm that it’s the case also for the third example:

```
>>> 3.4 / 2.2
1.5454545454545452
>>> 3.4 - 2.2
1.1999999999999997
```

So, 3.4 % 2.2 is 1.1999999999999997 because 3.4 equals 1*2.2 + 1.1999999999999997.

```
>>> 1*2.2 + 1.1999999999999997
3.4
```

Makes sense?

## Modulo Where Dividend is Smaller Than the Divisor

In the examples we have seen so far, the **dividend (left side of the modulo operator) **was always bigger than the **divisor (right side of the modulo operator)**.

Now, we will see what happens if the dividend is smaller than the divisor.

```
>>> 2 % 10
2
>>> 3 % 5
3
```

In both examples, the result of the division is 0 and hence the modulo is equal to the value of the dividend.

What about with floats?

```
>>> 2.2 % 10.2
2.2
>>> 3.4 % 6.7
3.4
```

That’s the same for floats.

## Using Python Modulo within an If Statement

One typical use of the Python modulo operator is a program that given a list of numbers prints odd or even numbers.

This is based on the following logic:

- An
**odd**number divided by 2 gives a remainder of 1. - An
**even**number divided by 2 gives a remainder of 0.

```
>>> for x in range(20):
… if x % 2 == 0:
… print("The number {} is even".format(x))
… else:
… print("The number {} is odd".format(x))
…
The number 0 is even
The number 1 is odd
The number 2 is even
The number 3 is odd
The number 4 is even
The number 5 is odd
The number 6 is even
The number 7 is odd
The number 8 is even
The number 9 is odd
The number 10 is even
The number 11 is odd
The number 12 is even
The number 13 is odd
The number 14 is even
The number 15 is odd
The number 16 is even
The number 17 is odd
The number 18 is even
The number 19 is odd
```

Notice that to print the value of x as part of the print statement we have used the **string format() method**.

## Python Modulo When the Divisor is Zero

Here is what happens when the divisor of an expression using the modulo operator is zero.

For integers…

```
>>> 3 % 0
Traceback (most recent call last):
File "", line 1, in
ZeroDivisionError: integer division or modulo by zero
```

For floats…

```
>>> 2.4 % 0
Traceback (most recent call last):
File "", line 1, in
ZeroDivisionError: float modulo
```

In both cases, the Python interpreter raises a **ZeroDivisionError** exception.

We can handle the exception using a **try-except statement**.

```
>>> try:
… 3 % 0
… except ZeroDivisionError:
… print("You cannot divide a number by zero")
…
You cannot divide a number by zero
```

## Using the Math.fmod Function

An alternative to the % modulo operator that is preferred when working with floats, is the **fmod() function** of the Python **math module**.

Below you can see the description of math.fmod() from the Python official documentation.

Before we have tested the % operator with negative integers:

```
>>> -5 % 2
1
>>> -5 % -2
-1
>>> 5 % -2
-1
```

Let’s see the results we get using math.fmod():

```
>>> math.fmod(-5, 2)
-1.0
>>> math.fmod(-5, -2)
-1.0
>>> math.fmod(5, -2)
1.0
```

And here is the difference when we calculate the modulo with negative floats…

**Modulo Operator used with negative floats**

```
>>> -5.2 % 2.2
1.4000000000000004
>>> -5.2 % -2.2
-0.7999999999999998
>>> 5.2 % -2.2
-1.4000000000000004
```

**Math fmod Python Function used with negative floats**

```
>>> math.fmod(-5.2, 2.2)
-0.7999999999999998
>>> math.fmod(-5.2, -2.2)
-0.7999999999999998
>>> math.fmod(5.2, -2.2)
0.7999999999999998
```

## Conclusion

We went through few examples that should help you use the Python modulo operator in your programs.

And you, what are you using the modulo for?

I’m a Tech Lead, Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!