Python Modulo operator

In Python and generally speaking, the modulo (or modulus) is referred to the remainder from the division of the first argument to the second.

The symbol used to get the modulo is percentage mark i.e. ‘%’.

In Python, the modulo ‘%’ operator works as follows:

  • The numbers are first converted in the common type.
  • A ZeroDivisionError exception is raised if the right argument is zero.
  • The arguments may be floating point numbers.
  • The result of using the ‘%’ operator always yields the same sign as its second operand or zero.
  • Also, the absolute value for the result is strictly smaller than the value of the second operand.

The section below shows using the modulo operator in Python.

An example of leap year with modulo operator

A leap year occurs once every fourth year. A leap year has 366 days where the number of days in February is 29. For example, 1992, 1996, 2000, 2004, 2008…2016 are leap years. To find out if a year is a leap year or not, you can divide it by four and if the remainder is zero, it is a leap year.

The Python program below asks the user to enter a year. There, I used modulo (%)and used the remainder value to display the result of the entered year. If the remainder is zero, it is a leap year otherwise not a leap year. You may copy the code and run in your IDE or see the graphic below for a few values entered:

#Example of modulo usage



year_input = int(input("Enter a year:  "))

if year_input % 4 == 0:

   print("Remainder is 0 so ", year_input, "is a leap year!")

else:

   print(year_input, "Not a leap year!")

The result:

Python modulo

Note: Leap year has two other conditions:

  • (i) Year is not divisible by 100. For example, 1900 is not a leap year, although it can be divided by 4.
  • (ii)Year can be divided by 400 is a leap year. For example, 2000 is a leap year as it can be divided by 400 (though it can be divided by 100 as well).
  • (iii)You need to incorporate these two conditions as well in the above code by using if..else if ladder etc.

Getting leap year with modulo in another way

In this example, I used filter function and used modulus operator there. A list of years is given and filter function will use the % operator and leap years will filter out as follows:

#Example of modulo usage with filter/lambda

Year_List = [1984, 1990, 1996, 2002, 2008, 2014, 2018]

L_Years = list(filter(lambda leap_yrs: (leap_yrs%4 == 0) , Year_List))

print("Following are leap years in the list: " ,L_Years)

The result:

Following are leap years in the list:  [1984, 1996, 2008]

Another example of using modulo operator

Still unclear about using the modulo (or modulus) operator. The following example should make things clearer.

There, I am simply using the two numbers with ‘%’ operator and assigning the remainder value to a variable. The variable value is displayed that should clarify how it works:

#Example of modulo operator



rem1 = 10%5

rem2 = 27%4

rem3 = 40%3

rem4 = 43%17


print("Remainder of 10%5 =: " ,rem1)

print("Remainder of 27%4 =: " ,rem2)

print("Remainder of 40%3 =: " ,rem3)

print("Remainder of 43%17 =: " ,rem4)

 

The output:

Remainder of 10%5 =:  0

Remainder of 27%4 =:  3

Remainder of 40%3 =:  1

Remainder of 43%17 =:  9

An example of modulo with floating point numbers

Just like the above example, I used floating point numbers with modulo operator and displayed the remainder:

rem1 = 21.5 % 5

rem2 = 100 % 5.5

rem3 = 10 % 2.2

rem4 = 50.5 % 5.5


print("Remainder of 21.5%5 = " ,rem1)

print("Remainder of 100%5.5 = " ,rem2)

print("Remainder of  10 % 2.2= " ,rem3)

print("Remainder of 50.5 % 5.5 = " ,rem4)

 

The result:

Remainder of 21.5%5 =  1.5

Remainder of 100%5.5 =  1.0

Remainder of  =  1.1999999999999993

Remainder of 43%17 =  1.0

Using negative numbers with modulus operator

This example uses negative numbers with the modulus operator.

#Example of modulo operator with negative numbers



rem1 = -18 % -3

rem2 = -25 % 5

rem3 = 30.6 % -6

rem4 = -35.5 % -5.5

print("Remainder of -18 % -3 = " ,rem1)

print("Remainder of -25 % 5 = " ,rem2)

print("Remainder of  30.6 % -6 = " ,rem3)

print("Remainder of -35.5 % -5.5 = " ,rem4)

The output:

Remainder of -18 % -3 =  0

Remainder of -25 % 5 =  0

Remainder of  30.6 % -6 =  -5.399999999999999

Remainder of -35.5 % -5.5 =  -2.5

How ZeroDivisionError is raised as using modulo operator?

As mentioned earlier, the ZeroDivisionError error is raised if the right argument is zero as using the modulo operator.

In the following example, I used 0 towards the left and right sides including integer and floating numbers:

rem1 = 0 % 15

rem2 = 0.0 % 25.5



print("Remainder of 0 % 15 = " ,rem1)

print("Remainder of 0.0 % 25.5  = " ,rem2)



rem3 = 15 % 0

rem4 = 25.5 % 0.0



print("Remainder of  15 % 0 = " ,rem3)

print("Remainder of 25.5 % 0.0 = " ,rem4)

The output:

Remainder of 0 % 15 =  0

Remainder of 0.0 % 25.5  =  0.0

Traceback (most recent call last):

rem3 = 15 % 0

ZeroDivisionError: integer division or modulo by zero

You can see, using zero in left side produce 0 result without any error. However, using zero on the right side raised an error.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!