# Python Modulo Operator - %

In this tutorial you will learn how to use the modulo (%) operator in Python.

The modulo operator (%) is very common in programming, as well as in mathematics. It means in Latin "a small measure".

The modulo operator returns the reminder of the division of the left operand (dividend) by the right operand (divisor). The operator can be useful in many cases - when you want to find if a number is even or odd; Determine whether a number can be divided by another number, etc. Here are some examples:

• 3 % 5 == 3. Since 3 = (5 X 0) + 3
• 8 % 5 == 3. Since 3 = (5 X 1) + 3
• 7 % 10 == 7
• 37 % 10 ==7

Watch the example:

``` #Modulo for i in range(13): print(i % 5) #Check if a number is even x = 5 print(x % 2 == 0) #Check if number is divided by 7 y = 28 print(y % 7 == 0) ```

## Python Modulo - Integers

When both operands are integers, the modulo operator returns the reminder of the division of the left integer by the right integer.

## Python Modulo - Floats

When one of the operands is a float, the modulo operator returns a float.

Please notice - modulo can have unexpected behavior when using floats. That is because of Python's floating-point error. You can read about it more here. Here is an example of such case:

``` #Modulo print(0.5 % 0.1) ```

The reason the result is 0.0999999… instead of 0.0 (as it should be) is because of the floating-point error.

### How to Fix Python's Floating-Point Error

If you want a more precise way of computing a modulo you can use the decimal module. Notice that the floats are wrapped as strings - this is a way to prevent Python from trying to round the numbers

``` # Decimal Modle import decimal print(decimal.Decimal('0.5') % decimal.Decimal('0.1')) ```

## Modulo with Negative Numbers

The modulo operator returns the reminder with the same sign as the divisor (the second operand). Meaning, that a positive divisor will return a positive float and vice versa. Watch the example.

``` # Modulo with Negative Numbers print(-5 % 3) # == 1 print(5 % 3) # == 2 print(5 % -3) # == -1 print(-5 % -3) # == -2 ```

### How to Use a C-like Modulo

When computing modulo with negative numbers, the behavior might be different than other programming languages. For instance, modulo in Python is different than modulo in C. Python rounds towards minus infinite and C towards 0. The modulo Python implements is called a “true” modulo. You can use the Math module to get a C like modulo.

``` # math.fmod import math print(math.fmod(-5, 3)) # == -2 print(math.fmod(5, 3)) # == 2 print(math.fmod(5, -3)) # == 2 print(math.fmod(-5, -3)) # == -2 ```

## Python Modulo - ZeroDivisionError

Modulo is not defined when division is not defined - meaning when we try to divide a number by zero. That's why when using modulo the second operand shouldn't be a zero, or else a ZeroDivisionError will be raised.

``` # ZeroDivisionError print(5%0) ```

## Modular Inverse (Reverse Modulo)

Given two integers - x, p. The modular inverse is the smallest integer y such that
(X * y) % p == 1.

Notice that in any case, y is smaller than p. In Python it is easy to compute the modular inverse using pow:
The syntax:

• pow(x, -1, p)

## Modulus vs Modulo - What's the difference?

Modulo is the operator - %. It returns the reminder of the division of the left operand (dividend) by the right operand (divisor).

Modulus is another way to call the divisor. X % p (X modulo P) - P is the modulus (or the divisor).

# Modulo in Practice

## How to Check if a Number is Even or Odd

It is a frequent task in the daily use of programmers. A beginner might try to program a complicated loop. However, using the modulo operator the task becomes easy! Watch the example:

``` # Modulo # Check if a number is even x = 5 print(x % 2 == 0) # Check if a number is odd print(5 % 2 == 1) ```

## How to find the Nth digit of a number in python

Here is another great way to practice a n awesome exercise in python. Here is how to do it:

``` # Modulo # Nth digit def get_n_digit(my_number, n): return my_number // 10**n % 10 print(get_n_digit(12345, 2)) ```

## Exercise - Caesar Cipher

Caesar cipher is a simple encryption technique. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. In this exercise try to encrypt numbers. Each will be substitute by a certain shift. Use the modulo operator to implement a cyclic shift. Good luck!

``` #Write your code here: ```