Modulo-Operator(%) in Python

In Python, different operators are used to perform various operations on different operands. Python categorizes different operators into different groups. The most commonly used operator groups are Arithmetic, Logical, Assignment, Bitwise operators, etc.

Python provides a “Modulo-operator (%)” that belongs to the arithmetic operators and is for getting the remainder of two numbers.

This guide will discuss the modulo operator in Python with the following outline:

What is Python Modulo-Operator (%)?

To return the remainder value of the two dividing numbers, the Modulo-operator (%) is used in Python. For instance, let’s look at the example:

8 % 3

The above expression will return “2” because by dividing the number “8” by “3”, the remainder will be “2”.

The Python modulo-operator is also used in various mathematical operations. In most cases, the modulo operator determines whether a number is even or odd.

Example 1: Basic Arithmetic Calculation Using Modulo-Operator

In the example code given below, the “modulo-operator %” is utilized to retrieve the remainder of two numbers:

Code:

print('Using Modulus Operator: ', 16 % 5)
print('Using "//" Operator: ', 16 // 5)

In the above code, two input numbers are divided using the “modulus %” operator and “//” operator.

Output:

The above output shows the remainder “1” when divided by the modulus operator and the quotient “3” when divided using the “//” operator.

Example 2: Using Modulo-Operator in User-Defined Function

In the code given below, the “modulo-operator” is used to check whether the input number is even or odd:

Code:

def even(a):
    return a % 2 == 0
def odd(b):
    return b % 2 != 0

print('Number is Even: ',even(15))
print('Number is Odd: ',odd(23))

In the above code, two functions named “even” and “odd” are defined. The modulo operator is utilized to obtain the remainder value of the input number by dividing it by “2”. Input numbers are even when the remainder is zero, otherwise, they are odd.

Output:

The above output shows a boolean value “True” when the condition is satisfied and “False” when the condition is not satisfied.

Example 3: Using Modulo-Operator in String

The “Modulo-Operator ” can also be used in a string for formatting the string value. In the example code given below, the “%” operator is used to place the variable’s value inside the string:

Code:

str_value = 'python'
int_value = 3
print('%s %d' % (str_value, int_value))

In the above code, the “%” is used with formatting characters “%s” and “%d” for formatting/concatenating the string and decimal value.

Output:

The output shows that the string value has been successfully concatenated with the integer value using the modulo operator.

Example 4: Python Modulo-Operator Exception

The only exception that appears while using the modulo-operator is the “ZeroDivisionError” in Python. The exception will be demonstrated in the below code example:

Code:

print('Using Modulus Operator: ', 16 % 0)

In the above code, the divisor number is “zero”, so it will throw the following error.

Output:

The output proves that a “ZeroDivisionError” occurs when we try to divide a number by zero.

Conclusion

In Python, the Modulo operator “%” divides the two numbers and retrieves the remainder as output. The modulo operator “%” is also used for string formatting in Python. The Python modulo-operator is used in basic arithmetic calculation and for checking the even and odd value. This article delivered a precise guide on how to use modulo-operators in Python.