NumPy Tutorial

Fast numeric computing with NumPy — arrays, broadcasting, and vector math.

1 min read 122 words Updated Mar 6, 2026

NumPy provides the n-dimensional array that nearly every Python data library builds on.

Arrays

import numpy as np

a = np.array([1, 2, 3, 4])
b = np.arange(10)          # 0..9
m = np.zeros((3, 3))
r = np.random.rand(2, 2)

Vectorized math

a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
print(a + b)              # [11 22 33]
print(a * 2)              # [2 4 6]
print(a @ b)              # dot product = 140

Broadcasting

m = np.ones((3, 3))
col = np.array([1, 2, 3]).reshape(-1, 1)
print(m + col)            # adds col to every row

Aggregation and indexing

x = np.random.rand(1000)
print(x.mean(), x.std(), x.max())
print(x[x > 0.9])         # boolean mask

Broadcasting lets you combine arrays of different shapes without explicit loops — the key NumPy superpower.