Skip to content

NumPy Array Operations

Vectorized NumPy operations for fast numerical computing in Python, with broadcasting examples.

NumPy lets you operate on entire arrays without writing loops.

Vectorized math

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b)        # [5 7 9]
print(a * 2)        # [2 4 6]

Broadcasting

matrix = np.array([[1, 2], [3, 4]])
print(matrix + np.array([10, 20]))
# [[11 22]
#  [13 24]]

Broadcasting aligns shapes automatically, which is both fast and concise.

AdSense — in-article slot

Related Reading