Before we start discussing Python Modulo Operator, let's first understand-What is a Modulo Operator?
The modulo operation is an arithmetic operation used to calculate the remainder of the division of one number by another. The remainder is known as the operation's modulus. It is a versatile operator that finds its application in various scenarios.
5 divided by 3 equals 1, with a remainder of 2, while 8 divided by 4 equals 2, with a remainder of 0.
In this tutorial, we will talk about the Modulo Operator. We will also address a few FAQs on Python Modulo Operator.
Use Python Modulo Operator
The modulo operator is represented by the percent sign in Python (%). The following is the syntax:
num1 % num2
Here is an example:
5 % 4
Output
1
If the divisor (the second argument) is equal to zero, a ZeroDivisionError is raised:
5 % 0
Output
ZeroDivisionError: integer division or modulo by zero
Floating numbers can also be used as arguments to the modulo operator:
6.8 % 3.4
Output
0.0
The percent character represents the interpolation operator when formatting strings.
Examples
The modulo operator is frequently used to determine whether a number is odd or even. If a number is divided by two and there is no remainder, it is an even number. Otherwise, if it leaves 1 as a remainder, the number is odd:
num = 11
if (num % 2) == 0:
print(num, "is even")
else:
print(num, "is odd")
When you run the code above, 11 percent 2 leaves a remainder of 1, and the following code is executed:
Output
11 is odd
Here's another example of how to use the modulo operator to determine whether a number is prime. A prime number is a positive integer that can only be divided by itself and by 1 without leaving a remainder.
def isPrimeNumber(num):
if num < 1:
return False
for i in range(2, num):
if (num % i) == 0:
return False
else:
return True
First, we check to see if the number, num, is positive. Then, without a reminder, we check whether the number is divisible by another number in the range of 2
to num
. The number is prime if none of the conditions are met.
Units of measurement can also be converted using the modulo operator. The example below demonstrates how to convert seconds to minutes:
def secondsToMinutes(sec):
seconds = sec // 60
minutes = sec % 60
return "%d minutes and %d seconds" % (minutes, seconds)
secondsToMinutes(657)
'57 minutes and 10 seconds'
The floor division operator with a double slash (//)
rounds the result to the nearest whole number.
FAQs on Python Modulo Operator
How does the modulo operator work?
The modulo operator %
returns the remainder of the division operation between two numbers. For example, 7 % 3
returns 1
since the remainder of dividing 7 by 3 is 1.
What is the syntax for using the modulo operator in Python?
The syntax is x % y
, where x
and y
are the numbers you want to divide and find the remainder.
What happens when the dividend is smaller than the divisor?
If the dividend is smaller than the divisor, the modulo result is the dividend itself. For example, 3 % 7
returns 3
.
Can the modulo operator be used with floating-point numbers?
Yes, the modulo operator can be used with both integer and floating-point numbers. The result will be a floating-point number.
What happens when dividing by zero using the modulo operator?
Division by zero is not allowed, including with the modulo operator. It raises a ZeroDivisionError
.
What are some common applications of the modulo operator?
The modulo operator is commonly used for tasks like checking for divisibility, finding even or odd numbers, repeating patterns, cycling through values, and indexing elements in circular data structures.
What does the expression x % 2 == 0
mean?
A common use of the modulo operator is to check if a number x
is even. The expression x % 2 == 0
returns True
if x
is divisible by 2, and False
otherwise.
Conclusion
We demonstrated how to use Python's modulo operator in this tutorial.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.