Module 15 · Python Standard Libraries — topic 12 of 13
statistics
Descriptive statistics with the standard library: mean, median, mode, standard deviation and variance (sample and population), quantiles, correlation, linear regression and the normal distribution.
Learning objectives
- Calculate averages and measures of spread
- Distinguish sample and population statistics
- Use quantiles, correlation, regression and NormalDist
Definition
statistics provides functions for calculating mathematical statistics of numeric data: averages, measures of spread, quantiles, correlation, simple linear regression and the NormalDist class.
Introduction
Class results, sales figures, sensor readings — you often need a quick summary. statistics gives accurate answers without installing anything.
Why it matters
Hand-written formulas are easy to get subtly wrong (sample vs population, even-length medians). The module handles these cases correctly.
Detailed explanation
mean is the average; median the middle value (average of the two middle values for even counts); mode the most common value. median_low/median_high and multimode cover special cases.
stdev and variance treat data as a sample (divide by n − 1); pstdev and pvariance treat it as the whole population (divide by n).
quantiles(data, n=4) returns cut points dividing data into n groups (quartiles for n=4).
correlation(x, y) gives Pearson's r between −1 and 1; linear_regression(x, y) returns the slope and intercept of the best-fit line.
NormalDist(mu, sigma) models a normal distribution; cdf(x) gives the probability of a value below x.
Empty data raises StatisticsError. Integer inputs may return Fractions or floats depending on the function.
For large datasets and advanced analysis, NumPy and pandas are faster, but statistics is perfect for everyday scripts.
Outliers affect statistics differently: one huge value pulls the mean a long way but barely moves the median, which is why median incomes are often reported instead of averages.
Syntax
- Averages and spread.
- Sample vs population functions.
- Regression and distributions.
Code examples
Averages
79.5 81.5 85
Spread
9.31 8.5 4.571428571428571
More examples
Quartiles, correlation and prediction
[70.5, 81.5, 86.5] 0.991 6.5 45.5 predicted score for 6 hours: 84.5 0.9332
Step-by-step explanation
- quantiles splits the marks into four groups and returns the three cut points.
- A correlation near 1 means study hours and scores rise together; the regression line predicts 84.5 for 6 hours.
- NormalDist.cdf gives the probability that a normally distributed score is below 85 (about 93%).
Expected output
Running “Quartiles, correlation and prediction” prints exactly:
Practical applications
- Summarising exam results or survey answers.
- Simple forecasting from trends.
Best practices
- Choose sample or population functions deliberately.
- Handle StatisticsError for empty data.
- Round results for display only.
Common mistakes
Using stdev for a whole population
Avoid
Prefer
stdev assumes a sample and slightly overestimates for complete populations.
Important notes
- Correlation doesn't prove causation — it only measures how closely two variables move together.
Key takeaways
- mean, median and mode summarise data.
- stdev/variance vs pstdev/pvariance.
- quantiles, correlation, regression and NormalDist go further.
Try it yourself
- 1Summarise a list of daily temperatures with mean, median, min, max and stdev.
- 2Fit a line to monthly sales and predict the next month.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is printed?