4.1 Arrays and Shape
import numpy as np
qty = np.array([2, 1, 2, 1, 1, 3])
print(qty.shape, qty.dtype) # (6,) int64
prices = np.array([32.0, 90.0, 120.0, 58.0, 1299.0, 65.0])
line_total = qty * prices # vectorised
print(line_total)
print(line_total.sum())
2-D example — fictional city × day order counts:
# rows: Pune, Nashik, Nagpur; cols: three days
counts = np.array([
[210, 230, 900], # Diwali spike on day 3
[120, 130, 400],
[150, 160, 450],
], dtype=np.int32)
print(counts.shape) # (3, 2) wait — (3, 3)
print(counts.mean(axis=1)) # mean per city
Steps in Jupyter
import numpy as np- Build
qtyandpricesas above. - Compute
line_total = qty * pricesandline_total.mean(). - Print
counts.sum(axis=0)(totals per day).
What you should see. Element-wise multiply without a Python for loop. Shape (3, 3) for the city-day grid.