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
| Parameter | Type | Description |
|---|---|---|
x1 | array_like | Dividend array. |
x2 | array_like | Divisor array. Must be broadcastable to the shape of x1. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array to store results. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying where to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing the modulus. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines 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
