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 is True, the resulting array will be set to the ufunc result. Otherwise, the resulting array will retain its original value. This is an optional parameter.
ufunc is short for universal function, and it operates on ndarrays(), 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 arrays
x1 = np.array([6, 6, 6])
x2 = np.array([2, 4, 6])
# implementing the divmod() function
myarray = np.divmod(x1, x2)
print(x1)
print(x2)
print(myarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 4 and 5: We create input arrays, x1 and x2, using the array() 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 x1 and x2to the console.
  • Line 12: We print myarray to the console. The myarray variable holds the result from the numpy.divmod() function implementation.

Free Resources