What is the numpy.divmod() function?
Overview
The divmod() function in Python obtains the quotient and remainder of the division two input arrays, x1, and x2, element-wise.
Syntax
numpy.divmod(x1, x2, /, out=None, *, where=True)
Syntax for the divmod() function
Parameter value
The divmod() function takes the following parameter values:
x1: This represents an input array of elements which are the dividends. This is a required parameter.
x2: This represents the divisor array. This is a required parameter.out: This represents the location where the result is stored. This is an optional parameter.where: This is the condition over which the input is being broadcast. At a given location where this condition isTrue, the resulting array will be set to theufuncresult. Otherwise, the resulting array will retain its original value. This is an optional parameter.
ufuncis short for universal function, and it operates onndarrays(), in an element-wise fashion. It also supports other standard features in NumPy.
**kwargs: This represents other keyword arguments. This is an optional parameter.
Return value
The divmod() function returns an array showing the element-wise quotient and remainder of the division of two arrays simultaneously.
Code
import numpy as np# creating input arraysx1 = np.array([6, 6, 6])x2 = np.array([2, 4, 6])# implementing the divmod() functionmyarray = np.divmod(x1, x2)print(x1)print(x2)print(myarray)
Explanation
- Line 1: We import the
numpymodule. - Line 4 and 5: We create input arrays,
x1andx2, using thearray()function. - Line 8: We implement the
numpy.divmod()function on the input arrays. We assign the result to a variable,myarray. - Line 10-11: We print the input arrays
x1andx2to the console. - Line 12: We print
myarrayto the console. Themyarrayvariable holds the result from thenumpy.divmod()function implementation.