# A Detailed Guide to numpy.remainder() Function in NumPy

Welcome back, readers! In our series on Python arithmetic operations, we bring you a new topic of remainder. The Numpy library provides us with a dedicated function to find the remainder in Python. We need to know this because the remainder helps us in tasks like checking divisibility, writing algorithms, distributing work equally among processors, etc.

In this tutorial, we will see how the numpy.remainder() function handles the remainder in Python, also see some interesting code examples to clear all your doubts about this function.

Also Read: How to Find the Product of Array Elements in Python

## Understanding Remainder

When we perform division, the leftover number that is not further divisible is called the remainder. According to math, the remainder is the amount left over after we subtract the product of the divisor and quotient from the dividend. Still confused? Let me get you clear with an example.

Imagine it’s your birthday, and you have received about 25 chocolates. But you have a younger sibling who insists on taking half of your chocolates. What will you do? Obviously, you will divide it between two, but is 25 completely divisible by 2? The fight between you and your sibling won’t end here, as 1 chocolate will remain undivided, this is called the remainder.

It is important to compute remainder as it’s useful in algorithms and programming, specifically, it plays an important part in cryptographic algorithms and also in hashing functions.

## Introducing numpy.remainder

In Python, Numpy offers us a numpy.remainder() that helps us compute remainders on provided input.

Now you might be thinking that this job of finding remainders can also be performed by using the modulo operator. Then what is the need for a separate function like this?

Well, let me be clear. This is because it correctly handles negative numbers by following the method of Euclidean division, while the modulo operator follows the convention of floor division, which may give different results for negative operands.

### Syntax:

The basic format to use the given function is given below:

```numpy.remainder(a, b)
```

In the given syntax,

• a: This is the number you want to divide (dividend). It can be an array of numbers or a scalar quantity.
• b: This is the number by which you want to divide (divisor). It can also be an array of numbers or a scalar quantity.

### Working:

• Input: It takes input of two scalar quantities or arrays: one is the dividend (a), and the other is the divisor (b).
• Process: For every element in a and b, it divides the element in a by the corresponding element in b to calculate the remainder. We refer to it as an element-wise operation.
• Output: It then returns an array that holds the remainders of each division.

## Examples of numpy.remainder in Python

Here are some basic examples to help you out more and enhance your understanding of numpy.remainder in Python.

### Example 1:

Let’s begin with using single values.

```import numpy as np

Chocolates = int(input("Enter the number of chocolates available: "))
siblings = 2

leftover = np.remainder(Chocolates,siblings)

print(f"Since {Chocolates} chocolates cannot be divided evenly between {siblings} siblings, they will fight over {leftover} chocolate.")
```

This code takes input for the number of chocolates available, then calculates how many chocolates are left over after dividing them evenly between the siblings using the remainder function and prints it.

### Example 2:

We can use a dividend array with a scalar divisor.

```import numpy as np

pencils = int(input("Enter the number of pencils: "))
erasers = int(input("Enter the number of erasers: "))
pens = int(input("Enter the number of pens: "))
items = np.array([pencils, erasers, pens])

students = int(input("Enter the number of Students you want to divide the stationary among: "))

leftover = np.remainder(items,students)

print(f"After even distribution of stationary items we are left with  {leftover[0]} pencils, {leftover[1]} erasers, {leftover[2]} pens.")
```

We create a dividend array input that holds the count of pencils, erasers, and pens. Next, we input the number of students. Then, by using the reminder function, we calculate the leftover count of each item and display it using the array indexing.

### Example 3:

Also, we can use a dividend array with a divisor array.

```import numpy as np

Blue_baloons = int(input("Enter the number of Blue Baloons: "))
Pink_baloons = int(input("Enter the number of Pink Baloons: "))
baloons = np.array([Blue_baloons, Pink_baloons])

Boys = int(input("Enter the number of Boys: "))
Girls = int(input("Enter the number of Girls: "))
Students = np.array([Boys, Girls])

leftover = np.remainder(baloons,Students)

print(f"After even distribution of baloons we are left with {leftover[0]+leftover[1]} baloons.")
```

This code takes the counts of blue and pink balloons(dividend array), as well as the counts of boys and girls(divisor array). It then calculates how many balloons are left over after distributing them evenly among the boys and girls together, and displays the total number of remaining balloons.

## Summary

And voila! We are done with one more important numpy arithmetic operation, i.e., the numpy remainder() function. In this guide, we have walked through the concept of the remainder, saw how we can find the remainder without using the modulo operator, and learned how this function works with different kinds of array data using simple examples. Now it’s your turn to experiment with this function on your own projects.

Also, check out How to Round Numbers in Python (5 Ways)

## Reference

https://numpy.org/doc/stable/reference/generated/numpy.remainder.html

Articles: 71