What is Python modulo used for?
The modulo operator in Python is used to return the remainder when performing division between two numbers. The modulo operator is simple and yet it is widely used in Python and programming in general.
Examples for Python modulo
To explore the functionality of the Python modulo operator we are going to create two integer variables. Besides the modulo operator, there are a number of other useful operators that perform different operations in Python.
For instance, the star sign is used to denote multiplication between two numbers.
We can also perform the division operation between two numbers using the forward slash. As shown below.
The Python modulo operator is considered an arithmetic operation and is denoted by the per cent sign. The modulo operator returns the remainder when one number is divided by another.
In the case above since the numbers are divisible, the remainder is zero. Because when we divide 10 by 2 the result is 5 and there is no remainder in this case.
However, If we change the divisor to three, we are going to get a remainder, since ten divided by three is not a whole number. So we actually have one leftover because three goes into 10 three times with one as the remainder.
Now the Python modulo operator is particularly useful in cases where we might want to work out whether a number is odd or even.
We can actually write a short if statement, that will compare two numbers and tell us whether a number is odd or even, based on the remainder given.
So normally we know that all even numbers are divisible by two and therefore the remainder is always zero, for example since 10 is an even number if we try to divide it by two the remainder is always zero.
However, if we try 11 which is an odd number we will definitely get a remainder. This is because all odd numbers are not divisible by two.
On the other hand, if we try 12 we definitely get a zero as the remainder, since 12 is an even number.
Using an if statement we can write a conditional statement, such that if the actual outcome a%b is zero then we’re going to print out an even number.
On the other hand, if the outcome of the Python modulo operation is not zero, we should print out odd.
So we know that in the case above we should get even since 12 is an even number. In this case, Python is letting us know that a dividend is an even number.
A better way of writing the above code would be using the divisor which is two in this case directly in the code since we do not intend to change it as shown below.
We can also try out a different number which in this case is going to return that the dividend, in this case, is an odd number.
This method is quite useful when testing out large numbers to ascertain whether they are even or odd.
Summary
If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
