Modulus in Python
Learn via video course
Overview
Modulus is an arithmetic operation that gives the remainder when the first number is divided from the second number. In Python, we achieve this operation using the modulus operator which is represented by the percentage symbol.
Scope of article
- In this topic, we will introduce ourselves to modulus in Python Programming.
- We will learn How to make a modulus symbol in Python?
- We will discuss the modulus operator (%) with some examples.
- we will see what is modulus (%) operator used for?
- Then we will discuss some alternatives to the modulus operator.
- Finally, we will look at the exception thrown by the modulus operator.
Introduction to Python Modulus
Just like other arithmetic operators: + , - , * , / , modulus '%' is another type of arithmetic operator.
What is Modulus in Python?
You must have seen the '%' symbol in mathematics. It denotes the percentage sign. In programming languages, we define it as a modulo operator.
Modulus is used as an arithmetic operator when you want to divide two numbers and want the remainder as the output.
Suppose you've two numbers 10 and 3 and you need to find the remainder when 10 is divided by 3. Here comes the function of the modulus operator where you can do 10%3 and get the result as 1.
Let's look at few basic examples:
Syntax of Modulus in Python
where a and b are two operands. Python allows the operands to be of type integer, or float.
Note:
b can never be 0. Because you can't divide a number by 0.
If b is given as 0, the compiler will return ZeroDivisionError.
Return value of Modulus in Python
The modulus operator gives the remainder as the output.
Modulo in Mathematics
Modulo operator has wide applications in the field of Mathematics. It can be used with integers, floating data types as well as negative values.
Let's see each one of them in detail:
Modulo operator using integers
The modulo operator, when used with two positive integers, will return the remainder of standard Euclidean division as:
Example:
Explanation:
- In the first case, when 15 is divided by 4 it leaves the remainder as 1. Hence, its output is 1.
- In the second case, 100 is exactly divisible by 2 and hence the output is 0.
Modulo operator using float
Just like the integers, the operands can be of float data type. The output of such operation is also of float type.
Example:
Explanation:
- In the first case, the operands are of float datatype and when 15.0 is divided by 4.0 it leaves the remainder as 1. Hence, its output is 1.
- In the second case, 10.0 is divided by 3.0 leaving the remainder as 1.0.
Modulo operator using negative operands
You've seen how the modulo operator works with positive operands. But things are different when the operands are negative.
Different languages like Python and other handles the negative operands in different ways!
In Python, the output will have the same sign as the denominator. If the denominator has a positive sign then the output is positive and when the denominator is negative, the output is also negative.
Example:
Explanation:
- In the first case, the divisor is negative and thus the output will be negative.
- While that of the second case where the dividend is negative and the divisor is positive, in simple words, with positive dividend, modular occurs like normal operation.
Modulo operator using divmod()
Python has a built-in function that exactly does the same work as the modulo operator. It is a part of python's standard library and can perform mathematical operations.
If integers are passed onto the function, then the Python compiler returns output in integers.
If floating numbers are passed onto the function, the output is also returned in floating data type.
If negative values are passed the remainder will take up the sign of the second operand same as the denominator.
Syntax
Parameters
It takes two parameters that represent the two operands.
Output
A tuple is an ordered sequence of elements enclosed in brackets.
Example:
Explanation:
- In the first case, when 15 is divided by 4, it gives the quotient as 3 and remainder also like 3. Hence, its output is a tuple with (3, 3).
- In the second case, 9.6 is divided by 2 leaving 1.5999999999999996 as the remainder.
Modulo operator using fmod()
Python has a built-in function called fmod() specifically for float operands.
It is part of the standard library under the math module.
Syntax
Parameters
It takes two parameters that represent the two operands of float data type.
Output:
Example:
Explanation:
- You've to import the math library for using the fmod() function.
- It takes two floating-point values as its arguments.
- In this case, 17.2 is divided by 1.1 leaving the remainder as 0.699999999999998.
Using Numpy
Numpy is one of the most used Python packages which enables us to perform several operations and write efficient code.
It has a function called remainder which gives the modulo of two operands. The inputs can be of any data type like integers, floating values. It also accepts positive and negative values.
Syntax:
Let's look at an example to output the remainder of two operands using the NumPy library.
Example:
Output:
Explanation:
- First import numpy package and naming it as np i.e., making an alias is optional.
- Take two operands on which you want to perform the operation.
- Use the correct syntax to generate the output.
- In the above example, when 15 is divided by 7 then it gives 1 as the remainder and hence its output.
Python Exception in Modulus operator
In Python, the compiler throws an exception whenever a number is divided by zero known as ZeroDivisionError. Therefore, in order to avoid the error, you shouldn't use zero in the denominator.
Let's see an example to see how Python exceptions in modulus operator work:
Output:
Explanation:
In the above example, try and catch block is used if in case the compiler throws an exception.
So, we try to divide a number by zero and see what the output looks like.
As the output can be seen, the Python compiler throws an exception stating that you cannot divide a number by zero when the modulo operator is used.
Conclusion
In this article, we came across various methods of using Modulus in Python. In fact, Python has built-in functions that work similar to the modulo operator. It is mainly used for time and pattern purposes.