If you’ve spent some time using Python, you’ll know that the language likes to use different symbols for arithmetic operations. Division is represented by a forward slash (/); multiplication uses an asterisk (*).
You’ll not be surprised to learn that the percentage sign (%) does not mean “percent” in Python. The percentage sign represents the modulo operator. It returns the remainder of dividing two numbers.
In this guide, we’re going to discuss what the modulo operator is and how it works. We’ll walk through an example of this operator to help you get started.
What is Modulo?
Modulo is a mathematical operation. It is used to calculate the remainder of a division sum.
The modulo operator is used like any other mathematical operator. Let’s calculate the remainder of 19 divided by 2:
print(19 % 2)
This code will divide 19 by 2 and return the remainder:
The number two can fit into 19 for a total of 8 times. There is one left over, which is our remainder. Let’s try out another example:
a = 9
b = 3
print(a % b)
We have assigned the numbers in our modulo operation to variables. “a” stores the value “9” and “b” stores the value 3.
Our code divides 9 by 3 and calculates its remainder. 3 goes into 9 for a total of 3 times. This means there is no remainder left. Our code returns: 0
We’ve calculated the remainder of a number in Python.
Modulo yields the remainder of a number in both floating point number division and integer division. Let’s try to find the remainder of a decimal number:
a = 9.2
b = 3
answer = round(a % b, 2)
This code calculates the remainder of 9.2 divided by 3. We use the
round() method to ensure our code returns a number that has two decimal places. The response is: 0.2. Our code has successfully calculated the remainder of these numbers.
Using Modulo to Find Odd and Even Numbers
The modulo operator allows you to determine if a number is odd or even.
Remember from math class that numbers divisible by two are even and any other number is odd. Knowing this, we can write a program that checks if a number is odd or even.
Let’s write a program that checks if the age of an elementary student is an odd or even number. We’ll start by asking a user to insert their age:
student_age = int(input(“What is your age? ”))
We use the
input() method to ask a user for their age. We use the
int() method to convert the value a user enters into a number.
Next, we’ll use an “
if” statement to check if the student’s age has any remainders:
if (student_age % 2) == 0: print("Your age is an even number!") else: print("Your age is an odd number!")
We use the modulo operator to check if there is a remainder left over after dividing a student’s age by two. If there is no remainder, the contents of our “if” statement are run. This will print out: “Your age is an even number!” to the console. Otherwise, the contents of our “else” statement are run.
Let’s run our program and insert the age 7:
What is your age? 7
Your age is an odd number!
Let’s see what happens if we insert the age 6:
What is your age? 6
Your age is an even number!
Our code successfully checks whether a number is odd or even.
The modulo operator calculates the remainder of a division sum. It is commonly used to check whether a number is odd or even. The modulo operator is represented using the percentage sign (%).
You can learn more about mathematical operators in Python by reading our guide to Python math operators. Now you’re ready to start using the modulo operator in Python like an expert developer!