NumPy mod()

The numpy.mod() function returns the element-wise remainder of division. It is equivalent to the Python modulus operator x1 % x2 and has the same sign as the divisor x2.

Syntax

</>
Copy
numpy.mod(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
x1array_likeDividend array.
x2array_likeDivisor array. Must be broadcastable to the shape of x1.
outndarray, None, or tuple of ndarray and None, optionalOptional output array to store results. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying where to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the modulus.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the element-wise remainder after division. If both x1 and x2 are scalars, a scalar is returned.


Examples

1. Computing Modulus of Two Numbers

Here, we compute the modulus of two scalar values.

</>
Copy
import numpy as np

# Define dividend and divisor
x1 = 10
x2 = 3

# Compute modulus
result = np.mod(x1, x2)

# Print the result
print("10 mod 3 =", result)

Output:

10 mod 3 = 1