Summarising Numbers
Totals, averages and extremes, and what each one hides.
The three you reach for first
sum, len and max answer most questions about a column. The average is sum divided by len, and it is worth writing out once so you remember it can divide by zero.
scores = [92, 88, 79]
print(sum(scores))
print(sum(scores) / len(scores))
print(max(scores))
An average is not the whole story
The same average can come from very different data. Reporting it next to the smallest and largest value costs one line and prevents most of the misreadings.
a = [50, 50, 50]
b = [0, 50, 100]
print(sum(a) / len(a), sum(b) / len(b))
print(min(b), max(b))
Exercise
Try It YourselfWrite a function average(scores) that returns the mean, and returns 0 for an empty list rather than raising.
Press Run to see output