AppDividend
Latest Code Tutorials

Python Modulo: How to Use Modulo Operator

The modulo operator in Python is considered an arithmetic operation, along with +, -, /, *, **, //. The modulo operator(%) is used to get the remainder after the division.

Python Modulo Operator

The modulo is a built-in operator that returns the remainder of dividing the left-hand operand by the right-hand operand. It’s used to get the remainder of the division problem.

When you see the % symbol, you may think “percent”. But in most programming languages, it means something different. For example, the % symbol in Python is called a Modulo Operator.

The modulo(%) operator yields a remainder from the division of the first argument by the second. The numeric arguments are first converted to the standard type. The zero-right argument raises the ZeroDivisionError exception.

The arguments might be floating numbers, e.g., 3.14%0.7 is equal to 0.34 (since 3.14 equals 4*0.7 + 0.34).

The modulo operator(%) always yields the result with the same sign as its second operand (or zero); the absolute value of a result is strictly smaller than the absolute value of the second operand [2].

In many programming languages, both operands of this modulo operator have to be integer. But Python Modulo operator is flexible in this case. The operands can be either an integer or float.

An expression like a % b evaluates the remainder of a ÷ b. Technically, it is a “modulus” instead of “reminder,” so results may differ if you compare with other languages where % is the remainder operator.

Syntax

x % y

Here, x is divided by y, and the remainder is returned. Let’s see an example with numbers.

print(5 % 2)

Output

1

The result of the previous example is one. So two go into five two times, and there is one leftover.

The 7 / 2 = 3 and Reminder 1 and 7 % 2 = 1. That means, the modulo returns reminder.

Let’s see another example.

See the following code.

print(6 % 2)

Output

0

Now, let’s see the following code in which the left value is less than the right value.

print(2 % 3)

Output

2

We got the 2 because 3 does not go into 2 any time, so the original 2 is leftover.

Using the Modulo Operator with range and for loop

The most common operation of Modulo Operator is to find even or odd numbers. For example, the code below uses the modulo operator to print all even numbers between 1 and 9.

for number in range(1, 9):
    if(number % 2 == 0):
        print(number)

Output

2
4
6
8

In this example, we have used the range() function to generate specific numbers and check if the value is even or not using the module operator.

Exceptions with the Python Modulo Operator

The only exception you get with a python modulo operator(%) is when the second argument is 0. This means that the divider operand of the modulo operator cannot be zero.

print(6 % 0)

Output

Traceback (most recent call last):
  File "app.py", line 1, in <module>
    print(6 % 0)
ZeroDivisionError: integer division or modulo by zero

We got the following error.

ZeroDivisionError: integer division or modulo by zero

It is one of the most common exceptions in all programming languages.

Formatting output using String modulo operator(%)

The modulo(%) operator can also be used for string format. It interprets a left argument much like the printf()-style format string applied to the right argument.

print("App: %2d, Dividend: %5.2f" % (1, 05.333))

Output

App:  1, Dividend:  5.33

There are two modulo operators in our example: The “%2d” and “%5.2f”. The general syntax for a format placeholder is the following.

%[flags][width][.precision]type

The modulo operator is also called a modulus operator.

See another example.

predestination = 'john_%(jane)s_'
predestination %= {'jane': 'doe'}
print(predestination)

Output

john_doe_

Conclusion

Python modulo operator, also known as the remainder operator, or integer remainder operator, works on the integers (and integer expressions) and yields a remainder when the first operand is divided by the second operand.

That’s it for this example.

See also

Python operators

Python ceil() and floor()

Python pi

Python sum()

Python sqrt()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.