Related Tags

modulo
python
operator

# What is a modulo operator (%) in Python?

The Python modulo operator, %, is used to retrieve the remainder after a division.

The basic syntax of the python modulo used in an equation is as follows:

# Syntax to get a remainder from a division in Python

argument1 % argument2 = remainder of (argument1/argument2)

## Examples

In this example, when we divide the first argument, 6, by the second argument, 2 - the result of the division is 3 with 0 as a remainder.

So in Python, 6 % 2 = 0

In this example, when we divide the first argument, 9, by the second argument, 7 - the result of the division is ${1}\frac{2}{7}$ with 2 as a remainder.

So in Python, 9 % 7 = 2

## Exceptions with the Python Modulo Operator

The only exception you get with a python modulo operator ā%ā is when the second argument is 0. This means that the divider operand of the modulo operator cannot be zero.

For example:

argument1 = 8
argument2 = 0

exceptionRemainder = argument1 % argument2
print(exceptionRemainder)

RELATED TAGS

modulo
python
operator