## Introduction to Python Remainder Operator

Python remainder operators are used for the computation of some operands. Operators are special symbols that are used on operands to do some operation such as addition, subtraction, division, etc. The operators can be symbolized as ‘+’ for addition, ‘-’ for subtraction, ‘/’ for division, ‘*’ for multiplication, etc. In Python, the modulus operator is a percentage symbol (‘%’) which is also known as python remainder operator, whereas there is a division operator for integer as ’//’, which works only with integer operands also returns remainder but in integers. Similarly, the python remainder operator or modulus operator also returns the remainder when two operands are divided, i.e. one operand is divided with other operand results in us remainder. This remainder operator is used for both integers and float numbers.

**Syntax:**

`x % y`

**Dividend % Divisor:** The remainder is obtained when x is divided by y. The remainder will be an integer if both dividends are integers. The remainder will be a floating-point number if one among dividend or divisor is a float number.

### Examples of Python Reminder Operator

Following are the different examples of Python Reminder Operator.

#### Example #1

**Code:**

```
x = 5
y = 2
r = x % y
print (‘Remainder is:’, r)
```

**Output:**

**Explanation:** In the above example x = 5 , y =2 so 5 % 2 , 2 goes into 5 two times which yields 4 so remainder is 5 – 4 = 1. In Python, the remainder is obtained using numpy.ramainder() function in numpy. It returns the remainder of the division of two arrays and returns 0 if the divisor array is 0 (zero) or if both the arrays are having an array of integers. This function is also used on individual numbers.

#### Example #2

**Code:**

```
import numpy as np
n1 = 6
n2 = 4
r = np.remainder(n1, n2)
print ("Dividend is:", n1)
print ("Divisor is:", n2)
print ("Remainder : ", r)
```

**Output:**

**Explanation:** The above example uses numpy.remainder() function on the given dividend and divisor to find the remains of two, which works similar to the modular operator. In this example, it is 6 % 4, 4 goes into 6, one time which yields 4 so the remainder is 6 – 4 =2.

#### Example #3

**Code:**

```
import numpy as np
arr1 = np.array([7, -6, 9])
arr2 = np.array([3, 4, 3])
rem_arr = np.remainder(arr1, arr2)
print ("Dividend array is: ", arr1)
print ("Divisor array is: ", arr2)
print ("Remainder array is : ", rem_arr)
```

**Output:**

**Explanation:** In the above example, the numpy.remainder() function can be used on the list of items to calculate the remainder of the respective item in the list or array of elements. we have two arrays [7 -6 9] and [3 4 3], so 7 % 3,3 goes into 7 two times, so the remainder is 1, -6 % 4, 4 goes into 6 one time, so the remainder is 2, 9 % 3, 3 goes into 9 three times, so the remainder is 0. The array of remainder values will be [1 2 0].

#### Example #4

A remainder operator or modulo operator is used to find even or odd numbers. Below is a piece of code to print odd numbers between 0 and 20.

**Code:**

```
for num in range(1, 20):
if(num % 2 != 0):
print(num)
```

**Output:**

**Explanation:** In the above example using a modulo operator, it prints odd numbers between 0 and 20 from the code; if the number is divided by 2 and the remainder obtained is 0, then we say it as an even number; else its odd number. If the number is 2, then 2 % 2 gives remainder 0, so its an even number, not odd, now; if the number is 3, then 3 % 2 gives remainder 1, which 2 goes into 3 one time so yields 2 and remainder is 3 – 2 =1 which not zero so the given number 3 is odd and using for loop it will check till 20 numbers and print all the odd numbers between 0 and 20. Modulo operator or Remainder operator is also used on floating-point numbers, not like division operator ( // ), which is used only on integers and gives remainder also in integer form.

#### Example #5

**Code:**

```
a = input("Dividend is :\n")
fa = float(a)
b = input("Divisor is :\n")
fb = float(b)
fr = fa % fb
print ("Remainder is",fr)
```

**Output:**

#### Example #6

In Python, the modulo operator can be used on negative numbers also which gives the same remainder as with positive numbers, but the negative sign of the divisor will be the same in the remainder.

**Code:**

`print(-5 % 3)`

**Output:**

**Code:**

`print(5 % -3)`

**Output:**

**Logic Behind the Code:**

- -5 % 3 = (1 -2*3) % 3 = 1
- 5 % -3 = (-1 * -2*-3) % 3 = -1

**Explanation: **These negative numbers use the fmod() function to find the remainder; if any one of the numbers among dividend or divisor is negative, then we can even use the fmod() function of the math library, and this can also be used to find the remainder of floating-point numbers also.

#### Example #7

**Code:**

```
import math
a = -10
b = 3
print(math.fmod(a,b))
```

**Output:**

**Explanation:** In Python, the modulo operator gives an error when the divisor is zero (0). Usually, it gives ZeroDivisionError as we know any number divided by zero is infinity (∞).

#### Example #8

**Code:**

```
p = 10
q = 0
r = p % q
print(r)
```

The above code gives us an error, as shown in the below screenshot.

**Output:**

**Code:**

```
p = 10
q = 0
try:
rem = p * q
print(rem)
except ZeroDivisionError as zde:
print("Cannot divided by 0")
```

This error can be caught by using try-except blocks, as shown in the below screenshot.

**Output:**

### Conclusion

In Python, the modulo operator is the operator to obtain the remainder of the division of two numbers known as dividend and divisor. This operator can be used to find the remainder of both integer and float data type numbers. The modulo operator is also one among the mathematical operators like addition ( +) , subtraction (- ), division( // ), etc. Division operator is used only on integer numbers, unlike modulo operator. And if the divisor is zero, we can handle it by exception handling using a try-except block to print the error.

### Recommended Articles

This is a guide to Python Reminder Operator. Here we discuss the Introduction to Python Reminder Operator along with examples and code implementation. You may also have a look at the following articles to learn more –