The percentage symbol (%) is used as a modulo operator in Python. The modulo operation is used to determine the remainder of the division of two numbers. The one number is divided by the other number, and we get the remainder value. Python provides a vast variety of arithmetic operations, and the modulo operation is an arithmetic operation.
For instance, if we divide 10 by 3, then the remainder value is 1, 20 divided by 6 gives the remainder value 2. The remainder value has also termed as the modulus.
The syntax of the modulo operation
The syntax of the modulo operation is as follows:
The numbers or operand could be the integer and float values. The first number (num1) is divided by the second number (num2), and the remainder value is returned by the modulo operation.
Examples of the modulo operation
The modulo operation is performed for various reasons, i.e., to determine the even or odd number, to check if the given year is a leap year or not, etc.
num1 = 19
num2 = 10
print("The remainder of 19/10 is:", num1%num2)
num1 = 5
num2 = 4.4
print("The remainder of 5/4.4 is:", num1%num2)
num1 = 3
num2 = 2
print("The remainder of 3/2 is:", num1%num2)
num1 = 20
num2 = 3.9
print("The remainder of 20/3.9 is:", num1%num2)
num1 = 20
num2 = 6
print("The remainder of 20/6 is:", num1%num2)
num1 = 5
num2 = 20
print("The remainder of 5/20 is:", num1%num2)
print("The remainder of 70/60 is:", 70%60)
Output
The output shows the remainder of different modulo operations.
If the divider operand is zero, then the Python interpreter throws a “ZeroDivisionError” error. Make that while performing the modulo operation, you do not make the divider operand zero.
num1 = 19
num2 = 0
print("The remainder of 19/0 is:", num1%num2)
Output
The Python interpreter throws an error.
The modulo operation can be performed on the negative numbers, and it works in the same way as it works on the positive numbers.
num1 = 19
num2 = -10
print("The remainder of 19/-10 is:", num1%num2)
num1 = -5
num2 = 4.4
print("The remainder of -5/4.4 is:", num1%num2)
num1 = 3
num2 = -2
print("The remainder of 3/-2 is:", num1%num2)
num1 = -20
num2 = 3.9
print("The remainder of -20/3.9 is:", num1%num2)
num1 = 20
num2 = -6
print("The remainder of 20/-6 is:", num1%num2)
num1 = -5
num2 = 20
print("The remainder of -5/20 is:", num1%num2)
print("The remainder of -70/-60 is:", -70%-60)
Output
Determining the leap year through modulo operation
The leap year is that year whose remainder value is zero when divided by 4. The leap year can easily be determined by performing the modulo operation.
year = input("Enter the year value\n")
#coverting year to an integer
year=int(year)
if (year%4==0):
print("The given year is a leap year")
else:
print("The given year is not a lear year")
Output
Conclusion
The modulo operation is used to find the remainder of the division of two numbers. Like other programming languages, the percentage symbol (%) is used as a modulo operator in Python. This article briefly explains the modulo operation in Python with examples.
from Linux Hint https://ift.tt/2TGeY0s
0 Comments