There are several uses for a Python modulo operation, including finding the remainder of a division and creating ciphers. The modulo operator (%) belongs to the arithmetic operations along with +, -, /, *, **, //. The remainder is returned after dividing the left operand/number by the right operand/number.
This post provides an in-depth guide on the Python modulo operator and “math.fmod()” function using various examples:
- Using Python Modulo – % Operator
- Performing Basic Calculation Using Modulo Operator
- Exception Handling Using % Operator
- Using math.fmod() Function
- Using math.fmod() to Get Modulo of Numbers
- Using math.fmod() on List Values
Using Python Modulo – % Operator
In Python, the modulo operator “%” is utilized to retrieve the remainder of the two divisible numbers. For instance, the number “10” divided by the number “3” will return the “1” as a remainder output. This remainder output will be found using the modulo operator “%.”
Let’s understand it by the following examples:
Note: You can also check this specific guide for an in-depth understanding of the Python modulo % operator.
Example 1: Basic Calculation Using Modulo Operator
The below code is used to perform a basic calculation using the modulo operator:
Code:
print(10%3)
print(9%3.0)
print(-5%3)
print(-10%3)
The “%” operator is used to find the remainder value of the positive and negative numbers.
Output:
Example 2: Exception Handling Using % Operator
The below code will throw a zero division error when the positive or negative number is divided by zero:
Code:
try:
print(15 % 0)
except ZeroDivisionError:
print("We cannot divide by 0")
The “try” block will execute the code and find the remainder of the number. If the “ZeroDivisionError” occurs, the “except” block handles the error.
Output:
The exception has been printed.
Using math.fmod() Function
The built-in function “math.fmod()” is used to find the remainder (modulo) of two given numbers as a float similar to the Python “%” operator. For example, math.fmod(5.5, 2) returns 1.5. However, it behaves differently from the modulo operator when dealing with negative or floating point numbers.
Let’s see different examples of the “math.fmod()” function:
Example 1:Using math.fmod() to Get Modulo of Numbers
The below code is used to find the remainder of a number using the “fmod()” function:
Code:
import math
print(math.fmod(15, 4))
print(math.fmod(-24, 7))
print(math.fmod(222, -7))
print(math.fmod(15, 6))
The “math.fmod()” function is used to return the remainder of the number by accepting the numerator and denominator as an argument.
Output:
The remainder has been returned successfully.
Example 2: Using math.fmod() on List Values
The below code is used to perform a modulo operation on elements of a list using the “math.fmod()” function:
Code:
import math
List = [-6, 8, -9, 3]
print(math.fmod(List[3], 2))
print(math.fmod(List[0], -4))
The “math.fmod()” takes the index value of the list as a first argument and divides it to the value of the second argument. This function returns the remainder value.
Output:
The remainder of the specific divisible number has been returned.
Conclusion
The Python modulo operator “%” and “math.fmod()” function of the math module is utilized to return the remainder of the two divisible numbers. The “math.fmod()” function is similar to the simple Python modulo operator “%” for positive numbers. But it behaves differently than the modulo operator when it comes to negative numbers or floating point numbers. This guide has presented an in-depth overview of the Python modulo operator and math.fmod() function using numerous examples.