Register Login

% (Modulo) Operator in Python

Operators play a significant role in Python programming. Returning the reminder value often plays a significant role in programs like reversing the digit or finding Cyclic reducdancy check (CRC) in network programming, or while developing Protocols using Python. We use % operator or modulus operator to return the remainder of two numbers. In this chapter, we will get to know about the modulus operator.

We will also learn how and where we can use this operator. This chapter will also teach you ways to find the odd or even numbers using the modulus operator.

Modulus Operator in Python

% in Python is called the Modulo operator. This is a binary operator that accepts two operands on both sides of it. The syntax for this operator is:

var_1 % var_2.

Example:

7 % 2

'One' is the result of this example as 7 % 2 gives '1' as remainder.

Example:

#Program to find odd number
for number in range(1, 10):
      if (number % 2 != 0):
             print(number)

Output:

% operator with int

Sometimes we have to use integer with the modulo operator. We get the remainder as standard when the modulo operator is with positive integers.

Example:

>>> 17 % 12

5

>>> 15 % 4

3

>>> 10 % 16

10

>>> 240 % 13

6

% operator with float

Like that of int, when we use the modulo operator with a float, it also gives the remainder. But, we will get a float value.

Example:

>>> 17.0 % 12.0

5.0

>>> 12.5 % 5.5

1.5

Modulo operator with divmod()

This function uses the modulo operator internally. It returns a tuple that contains results about floor division.

Example:

>>>  37 // 5

7

>>> 37 % 5

2

>>> divmod(37 , 5)

(7, 2)

% operator Precedence

We need to follow some rules while using the % operator. Like BODMAS rule in mathematics, programming also follows the precedence rule. The % operator shares the same quantity precedence as the (//), (*), (/) operators.

Example:

>>> 4 * 10 % 12 - 9

-5

Here, we see that both the modulo operator and multiplication have the same quantity precedence. If we want to override the supersede of additional operators, we will use parentheses for the surrounding operation.

Example:

>>> 4 * 10 % (12 - 9)

1

% operator for finding the odd and even number

This operator allows us to find if a number is odd or even.

Example:

student_age = int(input("what is your age"))

For checking any remainders in student's age, we will use the modulo operator of Python. Contents of the 'if' statement will run if no 'remainder' is there. Or else our else statement will run.

Let's insert age: 7 into our program.

Output:

% Modulus of a Negative Number

We can use the modulus operator of a negative number. To get a deep understanding, refer to the example below.

Example:

while True:
     x = input('Do you want to continue(Y/N)?')
     if x.upper() != 'Y':
        break
#input two integer number and store it into x and y
x = int(input('First number: '))
y = int(input('Second number: '))
print("Modulus of negative number is: ",x,"%",y," = ",x % y, sep = " ")
print("Modulus of negative number is: ",y,"%",x," = ",y % x, sep = " ")

Output:

Program to Reverse a number:

numb = 6280
rev_num = 0

while numb != 0:
    digit = numb % 10
    rev_num = rev_num * 10 + digit
    numb //= 10

print("The reversed number: " + str(rev_num))

Output:

Conclusion

In this tutorial, we have learned various ways to use the modulus operator in Python. We can use this operator with divmod(), int, and float. We have also got some idea of the precedence rule which we follow to avoid any errors in the program.

We also learned that we use the modulus operator to find the odd numbers in a given range and reverse a number using modulo operator. The real-world and industry-level use of this operator is endless.