The Python modulo operator calculates the remainder of dividing two values. This operator is represented by the percentage sign (%). The syntax for the modulo operator is: number1 % number2. The first number is divided by the second then the remainder is returned.
If you’ve spent some time using Python, you’ll know that the programming language likes to use different symbols for arithmetic operations. Division is represented by a forward slash (/); multiplication uses an asterisk (*).
You may not be surprised to learn that the percentage sign in Python (%) 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.
Python Modulo Operator
Modulo is a mathematical operation. It is used to calculate the remainder of a division sum. The Python modulo operator is the percentage sign (%). This sign finds the remainder from dividing the two numbers you specify.
The syntax for the modulo operator is:
example = 18 % 2
The modulo operator is placed between two numbers. In the above example, we assign the result of our sum to the Python variable “example”.
If there is no remainder after dividing the first number by the second, the modulo operator will return 0.
81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.
The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.
Let’s have a look at an example of how to use the modulo operator.
Modulo Python Example
We’re building an application to help young people learn how to use remainders. Our program asks a user for two numbers and then divides the first by the second and returns the remainder.
To start, let’s ask the user for two numbers:
n1 = float(input("Please insert a number: ")) n2 = float(input("Please insert the number you want to divide by the previous number: "))
We ask the user for two values and convert each of them to a floating-point number. We do this using a Python input() statement. Now, we can use the modulo operator to calculate the remainder from dividing the two numbers a user specifies:
print(n1 % n2)
Our program is now ready to use.
Let’s calculate the remainder of 19 divided by 2:
Please insert a number: 19 Please insert the number you want to divide by the previous number: 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.
Python Modulo Operator with Integers
Modulo yields the remainder of a number in both floating-point number division and integer division. In our last example, we converted each number a user inserts into our program into a floating-point value.
Let’s try to find the remainder of an integer:
a = 9 b = 3 answer = a % b print(answer)
This code calculates the remainder of 9 divided by 3. The response is: 0. This is because 9 divides by 3 equally. 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 Python int() method to convert the value a user enters into a number.
Next, we’ll use a Python if statement to check if the student’s age has any remainders:
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:
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
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 Python modulo operator is represented using the percent 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!
Do you want to learn more about coding in Python? Check out our complete How to Learn Python guide. You’ll find expert advice on what to learn and a list of top resources to help advance your learning.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.