Averages and spread: mean, median and standard deviation
Why this matters
Data rarely arrives as one number. Delivery times, order values, a model's scores on a test: they come as lists, and you need to say what's typical and how much the values vary. The mean, the median and the standard deviation are the three numbers people reach for, and each one has a trap. One huge order can drag the "average" order value far from any real order, and two products can share an average and behave nothing alike. This lesson builds each one by hand in Python, so you know exactly what a library means when it reports them.
- Compute a mean and a median by hand in Python, and say which one a situation needs
- Explain how one extreme value pulls the mean but not the median
- Measure spread with the range and the standard deviation
- Read a result like "82% ± 3" and spot a suspicious average
orders = [20, 25, 30, 25, 400] print(sum(orders) / len(orders))
The mean: share it out equally
The mean is what most people call "the average": add everything up and divide by how many there are. Think of it as sharing out equally. If five friends' restaurant bills add up to $100, the mean is what each would pay if they split it evenly: $20.
bills = [18, 25, 12, 30, 15] mean = sum(bills) / len(bills) print(mean)
The mean has a useful property: the mean times the count gives back the total. That makes it the right choice when you care about totals, like revenue per customer to plan next month's revenue. Its weakness is that every value pulls on it, and extreme values pull hardest. One $400 order among $25 ones moves the mean a long way, as the opening question showed. And an empty list has no mean at all: sum([]) / len([]) divides by zero, so code that averages data it didn't check first can crash on a quiet day.