Let's combine everything you've learned so far — array creation, arithmetic, aggregation, slicing — into a practical mini project. We'll take monthly sales data and compute the total, average, best month, and growth percentage. You'll see what a basic data analysis flow looks like when written with NumPy.
import numpy as np
# Monthly sales for one shop
months = np.array(["Jan", "Feb", "Mar", "Apr", "May", "Jun"])
sales = np.array([120000, 135000, 128000, 160000, 175000, 190000])
# Basic summaries
total_sales = sales.sum()
average_sales = sales.mean()
best_month_index = sales.argmax()
# Month-to-month growth percentage
growth = (sales[1:] - sales[:-1]) / sales[:-1] * 100
print("Total sales:", total_sales)
print("Average sales:", round(average_sales, 2))
print("Best month:", months[best_month_index], sales[best_month_index])
print("Growth %:", np.round(growth, 2))sales.sum() calculates the total. sales.mean() gives the average. sales.argmax() returns the index of the highest sales figure, which we use to look up the matching month name. For the growth calculation, sales[1:] gives the values starting from February, and sales[:-1] gives the values from January through May. Comparing these two gives us the month-to-month growth percentage.
Total sales: 908000 Average sales: 151333.33 Best month: Jun 190000 Growth %: [12.5 -5.19 25. 9.38 8.57]Info
When you use slicing in a calculation, matching array lengths matters. sales[1:] and sales[:-1] both have 5 elements, so the element-wise calculation works cleanly.