Hit enter after type your search item

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:


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:


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:


 

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:


 

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.


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:


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.

This div height required for enabling the sticky sidebar