Mean, Median and Mode
When you have a pile of numbers, you often want one number that sums them up: a "typical" or "central" value. There are three common choices, the mean, the median and the mode, and they can tell very different stories about the same data. Knowing which to use is a practical skill for anyone who reads a dashboard.
The mean (average)
Add all the values and divide by how many there are:
Seven daily step counts (in thousands): 6, 8, 5, 9, 8, 7, 13.
The mean uses every value, which is good, but it also means one extreme value can drag it a long way.
The median (middle value)
Sort the values and take the one in the middle:
With an even number of values there are two middle values; the median is their mean. For 3, 5, 8, 10 the median is .
The mode (most common value)
The value that appears most often. In the step counts, 8 appears twice, so the mode is 8. A data set can have no mode (all values different) or several modes. The mode is the only one of the three that also works for non-numbers, like "the most common browser among our visitors".
In Python
The statistics module computes all three:
import statistics as st
steps = [6, 8, 5, 9, 8, 7, 13]
print("mean :", st.mean(steps))
print("median:", st.median(steps))
print("mode :", st.mode(steps))
print("median of [3, 5, 8, 10]:", st.median([3, 5, 8, 10]))
print("multimode:", st.multimode([1, 2, 2, 3, 3, 4]))
print("browser mode:", st.mode(["Chrome", "Firefox", "Chrome", "Safari"]))mean : 8
median: 8
mode : 8
median of [3, 5, 8, 10]: 6.5
multimode: [2, 3]
browser mode: Chrome
When they disagree: outliers
Here all three happened to agree. Real data is often skewed. Suppose an API endpoint served ten requests with these response times in milliseconds:
import statistics as st
latency = [42, 38, 45, 40, 39, 44, 41, 43, 40, 2100] # one very slow request
print("mean :", st.mean(latency), "ms")
print("median:", st.median(latency), "ms")
without = latency[:-1]
print("without the outlier -> mean", round(st.mean(without), 1), "median", st.median(without))mean : 247.2 ms
median: 41.5 ms
without the outlier -> mean 41.3 median 41
The one slow request pushes the mean to 247 ms, a value that no request actually came close to. The median, 41.5 ms, still describes a typical request. The median is robust: it barely moves when a few values are extreme.
| Measure | Uses | Sensitive to outliers? | Good for |
|---|---|---|---|
| Mean | every value | yes, strongly | symmetric data, totals ("") |
| Median | the middle position | no | skewed data: latencies, incomes, house prices |
| Mode | the most frequent value | no | categories, the "most common" case |
Tip: If the mean is much larger than the median, the data probably has a long tail of large values. That gap itself is a useful warning sign on a dashboard.
In software
- Latency dashboards report the median (called p50) and high percentiles like p95 and p99 rather than the mean, because a few very slow requests distort the mean and hide what most users experience. p99 means 99% of requests were faster than that value.
- Benchmarking: repeat a timing many times and report the median (or the minimum, as Python's
timeitdocumentation suggests); one run interrupted by the OS should not ruin the result. - Mean as a running total: an "average order value" or "average frame time" is easy to keep updated: store the sum and the count, and divide when you display it.
- ML: filling missing values with the median of a column is a common, outlier-safe default (see missing data).
For a deeper treatment, see Mean, Median and Mode in the Statistics course.
Exercises
Exercise 1: All three
Find the mean, median and mode of 4, 7, 2, 7, 10, 6.
Answer
Mean 6, median 6.5, mode 7.
Solution
Exercise 2: Missing score
Four test scores average 80. What must the fifth score be for the average of all five to be 83?
Answer
Solution
The four scores sum to . Five scores averaging 83 must sum to . The fifth is .
Exercise 3: Which measure?
For each, pick the best "typical value": (a) salaries at a company with one very highly paid CEO, (b) the most popular T-shirt size, (c) the daily temperature over a mild, steady week.
Answer
(a) median, (b) mode, (c) mean.
Solution
(a) The CEO's salary is an outlier that inflates the mean, so the median is more honest. (b) Sizes are categories; only the mode applies. (c) The values are symmetric with no outliers, so the mean is fine and uses all the data.
Exercise 4: Median by hand in code
Write my_median(values) without the statistics module and test it on [9, 1, 5] and [8, 2, 6, 4].
Answer
5 and 5.0.
Solution
Sort, then take the middle element, or the mean of the two middle elements when the length is even.
def my_median(values):
s = sorted(values)
n = len(s)
mid = n // 2
if n % 2 == 1:
return s[mid]
return (s[mid - 1] + s[mid]) / 2
print(my_median([9, 1, 5]), my_median([8, 2, 6, 4]))5 5.0
Test yourself
What is the median of 12, 3, 8, 5, 20?
- 9.6
- 8
- 5
- 12
A dashboard shows mean latency 300 ms and median latency 45 ms. What is the most likely explanation?
- The data is symmetric
- A few very slow requests pull the mean up
- The median was computed wrongly
- Most requests take about 300 ms
Which measure can be used for non-numeric data such as favourite colours?
- mean
- median
- range
- mode