Lesson 23 of 30
NumPy Fundamentals
Arrays, vectorised operations, broadcasting, and numerical computing basics.
Installing NumPy
pip install numpy
Creating Arrays
import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.zeros((3, 3)) # 3x3 zeros
c = np.ones((2, 4)) # 2x4 ones
d = np.arange(0, 10, 2) # [0,2,4,6,8]
e = np.linspace(0, 1, 5) # 5 equally-spaced values
Array Operations
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print(x + y) # [5 7 9]
print(x * 2) # [2 4 6]
print(x ** 2) # [1 4 9]
print(np.dot(x, y)) # 32 (dot product)
print(x.mean()) # 2.0
print(x.sum()) # 6
2D Arrays (Matrices)
m = np.array([[1,2,3],[4,5,6]])
print(m.shape) # (2, 3)
print(m[0, 2]) # 3 (row 0, col 2)
print(m[:, 1]) # [2 5] (all rows, col 1)
print(m.T) # transpose
ℹ️ Why NumPy?
NumPy operations run in compiled C code — often 100× faster than equivalent pure-Python loops on numerical data.