Python Numpy – Divide all the elements of array by a constant

Numpy Array – Divide all elements by a constant

Dividing a NumPy array by a constant is as easy as dividing two numbers. To divide each and every element of an array by a constant, use division arithmetic operator /. Pass array and constant as operands to the division operator as shown below.

b = a / c
Run

where a is input array and c is a constant. b is the resultant array.

Example

In the following python example, we will divide array a by a constant 3. The resulting array is stored in b.

import numpy as np

#2D array
a = (np.arange(8)*2).reshape(2,4)

#print array
print("The array\n",a)

#divide all the elements of array by constant
b = a / 3

print("\nAfter dividing by a constant\n",b)
Run