The Python Modulo Operator – What Does % Mean in Python


The modulo operator in Python calculates the remainder of dividing two numbers. The percentage sign represents this operator ( percent ).

The modulo operator has the following syntax:

number1 % number2

After dividing the first number by the second, the remainder is return.

If you’ve worked with Python before, you’ll know that the computer language supports different symbols for arithmetic operations. A forward-slash (/) denotes division, whereas an asterisk (*) denotes multiplication.

You might be shock to find that the percentage symbol (%) in Python does not represent “percent”. The modulo operator is represent by the percentage sign. It returns the remainder when two numbers are divided.

We’ll go through what the modulo operator is or What Does % Mean in Python and how it works in this article. To get you start with modulo operator, we’ll go through an example of this operator.

Python Modulo Operator

Modulo is a mathematical term that refers to a mathematical operation. It’s used to figure out how much of a division total is left over. The percentage sign is the Python modulo operator ( % ). This symbol calculates the remainder by dividing the two integers you provide.

The modulo operator has the following syntax:

number1 % number2

Between two integers, the modulo operator is use. The modulo operator will return 0 if there is no remaining after dividing the first integer by the second.

Example 01:

# inputs
a = 435
b = 23
# Stores the remainder obtained 
# when dividing a by b, in c
c = a % b      
print(a, "mod", b, "=", c, sep = " ")


simple example

This was a basic operation done by the modulo operator and a simple example of how to utilize the syntax. Let’s say we wish to get the remainder of any integer from 1 to n when divided by a constant k.

Example 02:

# function is defined for finding out 
# the remainder of every number from 1 to n
def findRemainder(n, k):
  for i in range(1, n + 1):
    # rem will store the remainder 
    # when i is divided by k.
    rem = i % k  
    print(i, "mod", k, "=", rem, sep = " ")
# Driver code
if __name__ == "__main__" :
# inputs
  n = 10
  k = 7
# function calling
  findRemainder(n, k)


modulo example


The only exception you’ll get when using the modulo function in Python is ZeroDivisionError. This occurs when the modulo operator’s division operand reaches zero. As a result, the right operand cannot be zero. To learn more about this python error, look at the code below.

Example 03:

# inputs
a = 14
b = 0
# exception handling
    print(a, 'mod', b, '=', a % b, sep = " ")
except ZeroDivisionError as err:
    print('Cannot divide by zero!' + ' Change the value of the right operand.')


Exception example

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn.

Suggested Articles:

  1. How to Check Python Version in Linux, Mac, & Windows
  2. Convert Int to String in Python by Using Four Different Methods

Leave a Comment