21 C
Jaipur
Friday, November 27, 2020

How to modulo in Python? – Linux Hint

Must read

How To Earn Money Via Google Task Mate In India [Get Referral Code]

Google has recently launched a new app called Task Mate that lets users earn money by completing simple tasks such as taking pictures or...

How To Perform Quick “Chrome Actions” From Chrome’s Address Bar?

If you use Google Chrome regularly, you know that a lot can be done or fixed in the Google Chrome settings. However, more often...

Indian Government Caps Fee and Surge Pricing of Ride-Hailing Apps like Uber and Ola

The Ministry of Road Transport and Highways has issued new guidelines for ride-hailing companies such as Uber and Ola that allow aggregators to charge...

How to Install and Setup Let’s Encrypt (Certbot) on Linux

Getting an HTTPS certificate for your website is not anymore an optional choice. If you are a website developer, you might know that Google...

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.

# a program to perform the modulo operation
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.

# a program to perform the modulo operation
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.

# a program to perform the modulo operation
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.

#declaring the year variable
year = input(“Enter the year valuen)
#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.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

How To Earn Money Via Google Task Mate In India [Get Referral Code]

Google has recently launched a new app called Task Mate that lets users earn money by completing simple tasks such as taking pictures or...

How To Perform Quick “Chrome Actions” From Chrome’s Address Bar?

If you use Google Chrome regularly, you know that a lot can be done or fixed in the Google Chrome settings. However, more often...

Indian Government Caps Fee and Surge Pricing of Ride-Hailing Apps like Uber and Ola

The Ministry of Road Transport and Highways has issued new guidelines for ride-hailing companies such as Uber and Ola that allow aggregators to charge...

How to Install and Setup Let’s Encrypt (Certbot) on Linux

Getting an HTTPS certificate for your website is not anymore an optional choice. If you are a website developer, you might know that Google...

India Wearables Shipments Hit Record High in Q3 Amidst Falling Prices, Increased Options

Shipments of smart wearables in India reportedly jumped 165% in the July-September quarter thanks to an increased emphasis on remote work and rising health...