Last Updated : 10 Jul, 2025
numpy.mod() function returns the element-wise remainder of division between two arrays. It works like the modulo (%) operator but supports broadcasting and array operations.
Example:
Python
import numpy as np
a = np.array([10, 20, 30])
b = np.array([3, 7, 9])
res = np.mod(a, b)
print(res)
Explanation: Computes the remainder for each element: 10 % 3 = 1, 20 % 7 = 6, 30 % 9 = 3
Syntaxnumpy.mod(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
Parameters:
Returns: An array of remainders with the same shape as input arrays, element-wise.
ExamplesExample 1: Using scalar divisor
Python
import numpy as np
a = np.array([5, 8, 12])
res = np.mod(a, 4)
print(res)
Explanation: Remainders: 5 % 4 = 1, 8 % 4 = 0, 12 % 4 = 0
Example 2: Negative numbers
Python
import numpy as np
a = np.array([-5, -8, 7])
res = np.mod(a, 4)
print(res)
Explanation: In NumPy, mod always returns results with the sign of the divisor: -5 % 4 = 3, -8 % 4 = 0, 7 % 4 = 3.
Example 3: Using broadcasting
Python
import numpy as np
a = np.array([[10, 20], [30, 40]])
b = 6
res = np.mod(a, b)
print(res)
Explanation: Each element is divided by 6 and the remainder is returned.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4