NumPy divmod()
The numpy.divmod() function returns the quotient and remainder of element-wise division simultaneously.
It is equivalent to (x // y, x % y) but optimized for performance.
Syntax
</>
Copy
numpy.divmod(x1, x2, [out1, out2], /, [out=(None, 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. If x1.shape != x2.shape, they must be broadcastable to a common shape. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output arrays where the results are stored. If None, new arrays are created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original values. |
casting | str, optional | Defines the casting behavior when computing the quotient and remainder. |
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 a tuple (quotient, remainder). Both are arrays or scalars depending on input type.
Examples
1. Computing Quotient and Remainder for Single Values
Here, we compute the quotient and remainder of two scalar values.
</>
Copy
import numpy as np
# Define dividend and divisor
x = 10
y = 3
# Compute quotient and remainder
quotient, remainder = np.divmod(x, y)
# Print results
print("Quotient:", quotient)
print("Remainder:", remainder)
Output:
Quotient: 3
Remainder: 1
