Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Quotient and Mod

The divmod() function returns both the quotient and the remainder. The output consists of two arrays: the first array contains the quotients, while the second array contains the remainders.

Example

Return the quotient and remainder:

import numpy as np

arr1 = np.array([102030405060])
arr2 = np.array([3798233])

newarr = np.divmod(arr1, arr2)

print(newarr)
The example above will return:
(array([3, 2, 3, 5, 25, 1]), array([1, 6, 3, 0, 0, 27]))
The first array represents the quotients (the integer results of dividing 1010 by 332020 by 773030 by 99, and so on), while the second array represents the remainders from the same divisions.

Absolute Values

Both the absolute() and abs() functions perform the absolute operation element-wise, but it is advisable to use absolute() to avoid confusion with Python’s built-in math.abs().

Example

Return the quotient and remainder:

import numpy as np

arr = np.array([-1, –2123, –4])

newarr = np.absolute(arr)

print(newarr)