NumPy arrays, vectorisation and broadcasting
import numpy as np
prices = np.array([390.4, 475.67, 169.17, 689.69])
qty = np.array([2, 1, 5, 1])
revenue = prices * qty # element-wise, no loop
print(revenue.sum(), revenue.mean(), revenue.max())
inc_gst = np.round(prices * 1.1, 2)
expensive = prices[prices > 400] # boolean masking
m = np.arange(12).reshape(3, 4) # 3x4 matrix
m - m.mean(axis=0) # broadcasting: subtract each column's mean
Vectorised operations run in compiled code — often 50–100× faster than Python loops.
Rating
0
0
There are no comments for now.