Add a NumPy array and Python integer - Arithmetic operations of NumPy array and Python numbers

A NumPy array can be added to Python numbers.

import numpy

a = numpy.array([1, 2, 3])
b = a + 1

print(b)  # [2 3 4]

Divide a NumPy array by a Python number

Similarly, you can add, subtract, multiply, divide a NumPy array by a Python number.

import numpy

a = numpy.array([1, 2, 3]) + 5
b = numpy.array([1, 2, 3]) - 5
c = numpy.array([1, 2, 3]) * 2
d = numpy.array([1, 2, 3]) / 5

print(a)  # [6 7 8]
print(b)  # [-4 -3 -2]
print(c)  # [2 4 6]
print(d)  # [0.2 0.4 0.6]

Divide a Python number by a NumPy array

import numpy

a = 5 + numpy.array([1, 2, 3])
b = 5 - numpy.array([1, 2, 3])
c = 2 * numpy.array([1, 2, 3])
d = 5 / numpy.array([1, 2, 3])

print(a)  # [6 7 8]
print(b)  # [4 3 2]
print(c)  # [2 4 6]
print(d)  # [5.         2.5        1.66666667]

NumPy Array

NumPy Tutorial