# Numpy mod – Python NumPy mod() Function

NumPy mod() Function:

Numpy mod: The mod() function of the NumPy module returns the remainder of the division element-by-element.

In terms of array broadcasting, it is equivalent to l1 % l2.

When l2 is 0 and both l1 and l2 are arrays of integers, it gives 0.

Syntax:

numpy.mod(l1, l2, out=None)

Parameters:

l1 and l2: (Required)

These are required arguments. These are the arrays that have to be divided, here l1 as dividend and l2 as the divisor.

They must be broadcastable to a common shape if l1.shape!= l2.shape.

out:

This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.

Return Value:

The element-by-element remainder of dividing l1 and l2 is returned(l1%l2).

## NumPy mod() Function in Python

Example

Approach:

• Import NumPy module using the import keyword.
• Pass some random list as an argument to the array() function to create an array.
Store it in a variable.
• Create some sample arrays of different shapes to test the mod() function.
• Pass the first array and some random number to mod() function of NumPy module and print the result.
• Here it divides each element of the array1 with the given random value say 9 and gives the remainder.
• Pass the first array and second array to mod() function of NumPy module and print the result.
• Here it divides second array elements for the first array element and gives the remainder
• Similarly, test it with other arrays of different shapes.
• The Exit of the Program.

Below is the implementation:

# Import NumPy module using the import keyword.
import numpy as np
# Pass some random list as an argument to the array() function to create an array.
# Store it in a variable.
arry1 = np.array([[32,64],[95,128],[150,180]])
# Create some sample arrays of different shapes to test the mod() function.
arry2 = np.array([[10,20]])
arry3 = np.array([[100],[200],[300]])
arry4 = np.array([[55,65],[75,85],[95,105]])
# Pass the first array and some random number to mod() function of NumPy module and print the result.
# Here it divides each element of the array1 with the given random value say 9 and gives the remainder.
print('Getting mod value by dividing first array with 9: ')
print(np.mod(arry1, 9))
# Pass the first array and second array to mod() function of NumPy module and print the result.
# Here it divides second array elements for the first array element and gives the remainder
print('Getting mod value by dividing first array with second array: ')
print(np.mod(arry1, arry2))
# Similarly, test it with other arrays of different shapes.
print('Getting mod value by dividing first array with third array: ')
print(np.mod(arry1, arry3))
print('Getting mod value by dividing first array with fourth array: ')
print(np.mod(arry1, arry4))

Output:

Getting mod value by dividing first array with 9:
[[5 1]
[5 2]
[6 0]]
Getting mod value by dividing first array with second array:
[[2 4]
[5 8]
[0 0]]
Getting mod value by dividing first array with third array:
[[ 32 64]
[ 95 128]
[150 180]]
Getting mod value by dividing first array with fourth array:
[[32 64]
[20 43]
[55 75]]