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

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

When we see a ‘%’ the first thing that comes to our mind is the “Percentage-sign”, but when we think of it from the perspective of computer language, this sign has, in fact, another name and meaning. In computing, the modulo operation(%) finds the remainder or signed remainder after the division of one number by another (called the modulus of the operation).

Given two positive numbers, a and n, a modulo n (a % n, abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor.

Basically, Python modulo operation is used to get the remainder of a division. The modulo operator(%) is considered an arithmetic operation, along with +/***//. In most languages, both operands of this modulo operator have to be an integer. But Python Modulo is versatile in this case. The operands can be either integer or float.

Syntax:

```a % b

```

Here, a is divided by b, and the remainder of that division is returned.

Code:

 `# inputs` `a ``=` `13` `b ``=` `5` ` ` `# Stores the remainder obtained ` `# when dividing a by b, in c` `c ``=` `a ``%` `b      ` `print``(a, ``"mod"``, b, ``"="``,` `      ``c, sep ``=` `" "``)` ` ` `# inputs` `d ``=` `15.0` `e ``=` `7.0` ` ` `# Stores the remainder obtained ` `# when dividing d by e, in f` `f ``=` `d ``%` `e` `print``(d, ``"mod"``, e, ``"="``, ` `      ``f, sep ``=` `" "``)`

Output:

```13 mod 5 = 3
15.0 mod 7.0 = 1.0
```

This was a simple example showing the use of the syntax, and a basic operation performed by the modulo operator. Suppose, we want to calculate the remainder of every number from 1 to n when divided by a fixed number k.

 `# function is defined for finding out ` `# the remainder of every number from 1 to n` `def` `findRemainder(n, k):` `   ` `  ``for` `i ``in` `range``(``1``, n ``+` `1``):` `    ``# rem will store the remainder ` `    ``# when i is divided by k.` `    ``rem ``=` `i ``%` `k  ` `     ` `    ``print``(i, ``"mod"``, k, ``"="``, ` `          ``rem, sep ``=` `" "``)` ` ` `# Driver code` `if` `__name__ ``=``=` `"__main__"` `:` `   ` `  ``# inputs` `  ``n ``=` `5` `  ``k ``=` `3` `   ` `  ``# function calling` `  ``findRemainder(n, k)`

Output:

```1 mod 3 = 1
2 mod 3 = 2
3 mod 3 = 0
4 mod 3 = 1
5 mod 3 = 2
```

## Exceptions

The only Exception you get with python modulo operation is ZeroDivisionError. This happens if the divider operand of the modulo operator becomes zero. That means the right operand can’t be zero. Let’s see the following code to know about this python exception.

 `# inputs` `a ``=` `14` `b ``=` `0` ` ` `# exception handling` `try``:` `    ``print``(a, ``'mod'``, b, ``'='``,` `          ``a ``%` `b, sep ``=` `" "``)` `     ` `except` `ZeroDivisionError as err:` `    ``print``(``'Cannot divide by zero!'` `+` `          ``'Change the value of the right operand.'``)`

Output:

```Cannot divide by zero! Change the value of the right operand.
```

Last Updated on November 13, 2021 by admin

## Python | Pandas Series.astype() to convert Data type of seriesPython | Pandas Series.astype() to convert Data type of series

Pandas astype() is the one of the most important methods. It is used to change data type of a series. When data frame is made from a csv file, the columns are imported and data type is set automatically which

## Python | Pandas dataframe.max()Python | Pandas dataframe.max()

Pandas dataframe.max() Pandas dataframe.max() function returns the maximum of the values in the given object. If the input is a series, the method will return a scalar which will be the maximum of the values in the series. If the

## Python – Ways to convert string to json objectPython – Ways to convert string to json object

Python | Ways to convert string to json object Data send and get generally in a string of dictionary(JSON objects) forms in many web API’s to use that data to extract meaningful information we need to convert that data in

## Plotting Histogram in Python using MatplotlibPlotting Histogram in Python using Matplotlib

Plotting Histogram in Python using Matplotlib A histogram is basically used to represent data provided in a form of some groups.It is accurate method for the graphical representation of numerical data distribution.It is a type of bar plot where X-axis

## Convert Python Script to .exe FileConvert Python Script to .exe File

Convert Python Script to .exe File We create lots of Python programs per day and want to share them with the world. It is not that you share that Python program with everyone, and they will run this script in

## PUT method – Python requestsPUT method – Python requests

PUT method – Python requests Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make PUT request to a specified URL using requests.put() method. Before checking

## Python – Save List to CSVPython – Save List to CSV

Python – Save List to CSV A CSV (Comma Separated Values) is a simple file format, used to store data in a tabular format. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a

## Hotel Management Project in PythonHotel Management Project in Python

Hotel Management Project in Python This Project on Hotel Management is a general software developed (using Python) to simplify hotel operations by automating them. In this project, “AnCasa” is the project’s hotel name. It covers major aspects of hotel management;