Open In App

Modulo operator (%) in Python

Last Updated : 25 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

When we see a ‘%’ the first thing that comes to our mind is the “percent” but in computer language, it means modulo operation(%) which returns the remainder of dividing the left-hand operand by right-hand operand or in layman’s terms it finds the remainder or signed remainder after the division of one number by another.

Given two positive numbers, a and n, a modulo n (a % n, abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor.

The Python Modulo Operator

Basically, the Python modulo operation is used to get the remainder of a division. The modulo operator(%) is considered an arithmetic operation, along with +, , /, *, **, //. In most languages, both operands of this modulo operator have to be an integer. But Python Modulo is versatile in this case. The operands can be either integers or floats.

Syntax:

a % b

Here, a is divided by b, and the remainder of that division is returned.

Modulo Operator with Integer

Stores the remainder obtained when dividing a by b, in c

Python3




# inputs
a = 13
b = 5
 
c = a % b
print(a, "mod", b, "=",
      c, sep=" ")


Output:

13 mod 5 = 3

Modulo Operator With float with a negative number.

Stores the remainder obtained when dividing d by e, in f. For more examples, refer to How to Perform Modulo with Negative Values in Python.

Python3




# inputs
d = 15.0
e = -7.0
 
f = d % e
print(d, "mod", e, "=",
      f, sep=" ")


Output:

15.0 mod -7.0 = -6.0

Example using the Modulo Operator

Suppose, we want to calculate the remainder of every number from 1 to n when divided by a fixed number k.

Python3




# 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 %
     
    print(i, "mod", k, "=",
          rem, sep = " ")
 
# Driver code
if __name__ == "__main__" :
   
  # inputs
  n = 5
  k = 3
   
  # function calling
  findRemainder(n, k)


 Output:

1 mod 3 = 1
2 mod 3 = 2
3 mod 3 = 0
4 mod 3 = 1
5 mod 3 = 2

ZeroDivisionError in Python

The only Exception you get with the Python modulo operation is ZeroDivisionError. This happens if the divider operand of the modulo operator becomes zero. That means the right operand can’t be zero. Let’s see the following code to know about this Python exception.

Python3




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


 Output:

Cannot divide by zero! Change the value of the right operand.


Previous Article
Next Article

Similar Reads

Benefits of Double Division Operator over Single Division Operator in Python
The Double Division operator in Python returns the floor value for both integer and floating-point arguments after division. C/C++ Code # A Python program to demonstrate use of # "//" for both integers and floating points print(5//2) print(-5//2) print(5.0//2) Output: 2 -3 2.0 The time complexity of the program is O(1) as it conta
2 min read
Python - Modulo of tuple elements
Sometimes, while working with records, we can have a problem in which we may need to perform a modulo of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. Method #1: Using zip() + generator expression The combination of the above functions can be used to perform this task. In this
8 min read
Python - Lists Modulo
Sometimes we come across the situations in which we require to apply a particular function to each elements of two lists at similar index. These are quite similar and come up as application for certain utilities. Let’s discuss certain ways in which the modulo, i.e remainder of two lists can be performed. Method #1: Using zip() + list comprehension
4 min read
Python | Modulo K List
While working with the python lists, we can come over a situation in which we require to modulo the integer k to each element in the list. We possibly need to iterate and modulo k to each element but that would increase the line of code. Let’s discuss certain shorthands to perform this task. Method #1 : Using List Comprehension List comprehension i
3 min read
Python | Maximum modulo pair
Sometimes, we need to find the specific problem of getting the pair which yields the maximum remainder. In some case, we don’t with to change the ordering of list and perform some operation in the similar list without using extra space. Let’s discuss certain ways in which this can be performed. Method #1 : Using list comprehension + max() + combina
3 min read
Python - Modulo K elements removal
Due to the upcoming of Machine Learning, focus has now moved on handling the certain values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of certain values in essential and knowledge of it is a must. Lets discuss certain ways in whic
6 min read
Python | K modulo on each Dictionary Key
Sometimes, while working with dictionaries, we might come across a problem in which we require to perform a particular operation on each value of keys like K modulo on each key. This type of problem can occur in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using loop This is the naive method in
4 min read
Python | Nested Records Modulo
Sometimes, while working with records, we can have a problem in which we require to perform index wise remainder of tuple elements. This can get complicated with tuple elements to be tuple and inner elements again be tuple. Let’s discuss certain ways in which this problem can be solved. Method #1 : Using zip() + nested generator expression The comb
5 min read
How to perform modulo with negative values in Python?
Taking the modulo of a negative number is a bit more complex mathematics which is done behind the program of Python. If we don't understand the mathematics behind the modulo of negative numbers then it will become a huge blunder. Mathematics behind the negative modulo : Let's Consider an example, where we want to find the -5mod4 i.e. -5%4. You all
3 min read
Python Modulo String Formatting
In Python, a string of required formatting can be achieved by different methods. Some of them are; 1) Using % 2) Using {} 3) Using Template Strings In this article the formatting using % is discussed. The formatting using % is similar to that of 'printf' in C programming language. %d - integer %f - float %s - string %x - hexadecimal %o - octal The
2 min read
Article Tags :
Practice Tags :