Skip to content
ComputerCS
Start learning

Data Analytics · Lesson 5.1.1–5.1.3

Basic Statistics: Central Tendency, Dispersion and Probability

Mean, median, extremes and spread — computed by hand to understand them, then confirmed with the statistics module.

01

Learning objectives

  • Compute mean, median, min, max and range by hand and verify each with Python.
  • Explain when the median is the fairer summary than the mean (outliers).
  • Compute variance as the average squared distance from the mean.
  • Build a frequency table with the dictionary counting pattern.
  • Apply round() only at reporting time and explain why.
  • Calculate a simple probability from equally likely outcomes and interpret it as a value from 0 to 1 or 0% to 100%.
02

Lesson notes

Five hundred marks tell a story no one can hear; three numbers can. The mean is the total divided by the count — the fair share. The median is the middle value after sorting, so half the data lies below and half above; with an even count, average the two middle values. min and max give the boundaries, and the range (max − min) gives a first measure of spread.

Compute them by hand first — it is examinable and it builds judgement. Mean of [62, 91, 78, 85, 70]: the total is 386 over 5, giving 77.2. Median: sort to [62, 70, 78, 85, 91] and take the middle, 78. Only when the recipe is muscle memory does the module version make sense: statistics.mean(data), statistics.median(data), min(data), max(data) — one line each, no loop required.

Mean and median disagree when data is skewed. Marks of [40, 45, 50, 55, 300]: the mean is 98 — higher than four of the five students — while the median stays an honest 50. One extreme value, an outlier, drags the mean but barely moves the median, which is why income statistics in the news quote medians. Exam rule: when an outlier is present, report the median, or at least mention it.

Two datasets can share a mean and differ wildly in scatter: [58, 60, 62] and [20, 60, 100] both average 60. Variance measures this: the average of the squared distances from the mean — for [2, 4, 6] it is ((−2)² + 0² + 2²) ÷ 3, about 2.67. Its square root, the standard deviation, comes back into the data's own units. Compute the variance by hand once, then let statistics.pvariance and statistics.pstdev do the chore.

Frequency tables answer the question 'which values occur how often?': loop the data and count with counts[g] = counts.get(g, 0) + 1 — chapter 6's dictionary pattern doing statistics. Board tip: long questions ask for the program and its output, so show the formula (mean = total ÷ n), then the Python line, then the printed result — and round only for display with round(value, 2), keeping full precision inside the calculation.

Probability describes how likely an event is to occur. For equally likely outcomes, probability equals the number of favourable outcomes divided by the total number of possible outcomes. A fair coin has two equally likely outcomes, so the probability of heads is 1/2 or 50%. Probability helps interpret uncertainty in surveys, experiments and real-world decisions; the assumptions about outcomes must be stated.

03

Worked code examples

By hand, then by module

The hand loop prints 8.0 because true division always yields a float; statistics.mean returns the exact mean as the int 8, and the median of sorted [6, 7, 8, 9, 10] is also 8.

import statistics

data = [7, 8, 9, 10, 6]
total = 0
for x in data:
    total = total + x
mean = total / len(data)
print(mean)
print(statistics.mean(data))
print(statistics.median(data))
Output
8.0
8
8

Extremes, range and mean

min and max bound the data; their difference 29 is the range; the total 386 over 5 students gives the mean 77.2.

import statistics

data = [62, 91, 78, 85, 70]
print(min(data), max(data))
print(max(data) - min(data))
print(statistics.mean(data))
Output
62 91
29
77.2

Spread: variance step by step

The distances from the mean are −2, 0 and 2; squared (4, 0, 4) and averaged over 3 values, the variance is 8/3 ≈ 2.67 — rounded only for display.

data = [2, 4, 6]
mean = sum(data) / len(data)
total = 0
for x in data:
    total = total + (x - mean) ** 2
variance = total / len(data)
print("mean:", mean)
print("variance:", round(variance, 2))
Output
mean: 4.0
variance: 2.67
04

Key terminology

mean

The total of the values divided by how many there are.

median

The middle value of sorted data; the average of the two middle values when the count is even.

outlier

An extreme value far from the rest, which drags the mean but not the median.

variance

The average of the squared distances from the mean — a measure of spread.

standard deviation

The square root of variance, back in the data's own units.

frequency table

A count of how often each distinct value occurs.

Probability

A numerical measure of how likely an event is to occur, from 0 (impossible) to 1 (certain).

05

Common misconceptions

Common mistake

The mean is always the fairest single summary.

Correct understanding

One extreme value can drag the mean far from typical; with outliers present, the median describes the middle honestly.

Common mistake

The median can be read off without sorting.

Correct understanding

The median is defined by position in sorted order — sort first, then take the middle (or average the middle two).

Common mistake

Variance is a kind of average of the data itself.

Correct understanding

Variance averages squared distances from the mean — it measures spread, not level, and its units are squared.

06

Check your understanding

1. statistics.median([7, 3, 9, 1]) returns —

2. Marks: [40, 45, 50, 55, 300]. Which summary best describes the typical student?

3. statistics.median([2, 4]) returns —

4. A fair coin is flipped once. What is the probability of getting heads?

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.