The Wayback Machine - https://web.archive.org/web/20250131014947/https://www.geeksforgeeks.org/what-is-a-modulo-operator-in-python/
Open In App

Modulo operator (%) in Python

Last Updated : 25 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

Python
# 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.

Python
# 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.

Python
# 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 = 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.

Python
# 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.

Modulo operator (%) in Python – FAQs

What is the Modulo Operator Used For in Python?

The modulo operator % in Python is used to find the remainder of a division operation. Given two numbers, a and b, the expression a % b returns the remainder when a is divided by b.

Syntax:

remainder = a % b

How to Apply the Modulo Operator to Different Data Types in Python?

Integer Data Types:

result = 10 % 3
print(result) # Output: 1

Floating-Point Numbers:

result = 10.5 % 3
print(result) # Output: 1.5

When Applying to Strings (Modulo operator is not directly applicable to strings; use formatting methods instead):

formatted_string = "The result is %d" % 10
print(formatted_string) # Output: The result is 10

What Are Typical Use Cases for the Modulo Operator in Python?

Checking Even or Odd Numbers:

number = 15
if number % 2 == 0:
print("Even")
else:
print("Odd")

Cycling Through Indices (e.g., circular buffer):

indices = [0, 1, 2, 3]
current_index = 5
index = current_index % len(indices)
print(indices[index]) # Output: 1

Determining Divisibility:

number = 20
if number % 5 == 0:
print("Divisible by 5")

Implementing Time Calculations (e.g., converting minutes to hours and minutes):

total_minutes = 130
hours = total_minutes // 60
minutes = total_minutes % 60
print(f"{hours} hours and {minutes} minutes") # Output: 2 hours and 10 minutes

How Does the Modulo Operator Handle Negative Numbers?

The result of the modulo operation with negative numbers is determined by the sign of the divisor. In Python, the result of a % b has the same sign as the divisor b.

Positive Dividend:

result = 7 % -3
print(result) # Output: -2

Negative Dividend:

result = -7 % 3
print(result) # Output: 2

Is the Modulo Operator Applicable to Floating-Point Numbers in Python?

Yes, the modulo operator is applicable to floating-point numbers. It returns the remainder of the division, which can also be a floating-point number.

Example:

result = 10.5 % 3
print(result) # Output: 1.5


Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg