Statistics: Mean, Median, Mode, SD
Why statistics?
A list of 500 marks is hard to understand. Statistical measures summarise a whole data set in a few numbers: what is a typical value (central tendency — mean, median, mode) and how spread out the values are (variability — range, standard deviation). We will use the heights (cm) of 8 students:
Mean
Mean (average) = sum of all values ÷ number of values.
Sum = 150 + 155 + 160 + 160 + 162 + 165 + 170 + 175 = 1297; Mean = 1297 ÷ 8 = 162.125 cm.
Median
Median is the middle value after sorting. With an odd count it is the centre value; with an even count it is the average of the two middle values.
Mode
Mode is the value that occurs most often. Here 160 appears twice, so mode = 160. Mode is the only measure that also works for non-numeric data, e.g. the most popular shoe size or favourite colour.
Range
Range = maximum − minimum = 175 − 150 = 25 cm. It is quick but depends only on two extreme values.
Standard deviation
Standard deviation (σ) tells how far values are, on average, from the mean. Steps: (1) find the mean, (2) subtract the mean from each value, (3) square the differences, (4) average the squares — this is the variance, (5) take the square root — this is the SD.
| x | x − mean | (x − mean)² |
|---|---|---|
| 2 | -3 | 9 |
| 4 | -1 | 1 |
| 4 | -1 | 1 |
| 4 | -1 | 1 |
| 5 | 0 | 0 |
| 5 | 0 | 0 |
| 7 | 2 | 4 |
| 9 | 4 | 16 |
data = [2, 4, 4, 4, 5, 5, 7, 9]
mean = sum(data) / len(data)
squares = [(x - mean) ** 2 for x in data]
variance = sum(squares) / len(data)
sd = variance ** 0.5
print('Mean:', mean)
print('Squared deviations:', squares)
print('Variance:', variance, '| SD:', sd)All measures in Python
Method 1 — write the logic yourself (good for exams):
heights = [150, 155, 160, 160, 162, 165, 170, 175]
n = len(heights)
mean = sum(heights) / n
s = sorted(heights)
if n % 2 == 0:
median = (s[n // 2 - 1] + s[n // 2]) / 2
else:
median = s[n // 2]
mode = max(set(heights), key=heights.count)
rng = max(heights) - min(heights)
print('Mean :', mean)
print('Median:', median)
print('Mode :', mode)
print('Range :', rng)Method 2 — use the statistics module:
import statistics as st
heights = [150, 155, 160, 160, 162, 165, 170, 175]
print(st.mean(heights), st.median(heights), st.mode(heights))
print('Population SD:', round(st.pstdev(heights), 2))
print('Variance :', round(st.pvariance(heights), 2))Effect of outliers
An outlier is a value far away from the rest. It pulls the mean strongly but hardly moves the median.
import statistics as st
salary = [25, 28, 30, 32, 35, 250] # thousand rupees; one CEO salary
print('Mean :', round(st.mean(salary), 2))
print('Median:', st.median(salary))Two classes can have the same mean but very different spread — SD shows the difference:
import statistics as st
class_a = [60, 62, 64, 66, 68]
class_b = [40, 50, 64, 78, 88]
print('Means:', st.mean(class_a), st.mean(class_b))
print('SD A :', round(st.pstdev(class_a), 2))
print('SD B :', round(st.pstdev(class_b), 2))Which measure to use?
| Measure | Best when | Example |
|---|---|---|
| Mean | Numeric data without extreme values | Average marks of a class |
| Median | Data has outliers or is skewed | Typical salary, house prices |
| Mode | Most common category or value | Most sold shirt size |
| Range | Quick idea of spread | Max − min temperature of a day |
| Standard deviation | Exact measure of spread / consistency | Comparing consistency of two batsmen |
Summary & practice
- Mean = Σx ÷ n; median = middle of sorted data; mode = most frequent.
- Range = max − min; SD = √(Σ(x − mean)² ÷ n).
- Outliers affect the mean, not much the median.
- Python: statistics.mean, median, mode, pstdev, pvariance.
For the runs 12, 45, 45, 78, 30, 90, 45 find mean, median, mode and range by hand, then check with Python.
Answers: mean = 345 ÷ 7 ≈ 49.29, median = 45, mode = 45, range = 78.
