What is numpy.divide() in Python?

Python’s numpy.divide() computes the element-wise division of array elements. The elements in the first array are divided by the elements in the second array.

numpy.divide() performs true division, which produces a floating-point result.

Syntax

numpy.divide() is declared as follows:

numpy.divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'true_divide'>

In the syntax given above, x1 and x2 are non-optional parameters, and the rest are optional parameters.

A universal function (ufunc) is a function that operates on ndarrays in an element-by-element fashion. The divide() method is a universal function.

Parameters

The numpy.divide() method takes the following compulsory parameters:

  • x1 [array-like] - input array elements that act as the dividend.

  • x2 [array-like] - input array elements that act as the divisor. If the shapethe shape of an array is the number of elements in each dimension of x1 and x2 is different, they must be broadcastable to a common shape for representing the output.

The numpy.divide() method takes the following optional parameters:

Parameter

Description

out

represents the location into which the output of the method is stored.

where

True value indicates that a universal function should be calculated at this position.

casting

controls the type of datacasting that should occur. The same_kind option indicates that safe casting or casting within the same kind should take place. 

order

controls the memory layout order of the output function. The option K means reading the elements in the order they occur in memory.

dtype

represents the desired data type of the array.

subok

decides if subclasses should be made or not. If True, subclasses will be passed through. 

Return value

numpy.divide() returns the output of x1/x2 in an output array.

  • If both x1 and x2 are scalars, the return type is also scalar.

  • If x2 is zero, an error occurs.

Examples

The example below shows the result of dividing the elements in array arr1 by elements in array arr2:

import numpy as np
arr1 = np.array([2,3,4])
arr2 = np.array([5,7,2])
print (np.divide(arr1,arr2))

The example below shows the result of dividing the elements in array arr3 by elements in array arr4:

import numpy as np
arr3 = np.array([[1,2,3], [4,6,8]])
arr4 = np.array([[4,5,6], [2,3,8]])
print (np.divide(arr3,arr4))

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved