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

ParameterTypeDescription
x1array_likeDividend array.
x2array_likeDivisor array. If x1.shape != x2.shape, they must be broadcastable to a common shape.
outndarray, None, or tuple of ndarray and None, optionalOptional output arrays where the results are stored. If None, new arrays are created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original values.
castingstr, optionalDefines the casting behavior when computing the quotient and remainder.
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 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