Python statistics - mean, median, stdev, quantiles and correlation without NumPy
Answer: mean() returns the sample arithmetic mean, median() the middle value, and stdev() the sample standard deviation. The module calculates mathematical statistics of numeric data and is not intended to compete with NumPy or SciPy.
Examples
from statistics import (mean, fmean, median, mode, stdev, pstdev,
quantiles, correlation, linear_regression, NormalDist)
data = [1.5, 2.5, 2.5, 2.75, 3.25, 4.75]
mean(data) # 2.875
fmean(data) # always a float, faster
median(data) # 2.625 (mean of middle two)
mode(data) # 2.5
stdev(data) # sample standard deviation
pstdev(data) # population standard deviation
quantiles(data, n=4) # 3 cut points (quartiles)
x = [1, 2, 3, 4, 5]
y = [2, 4, 5, 4, 5]
correlation(x, y) # Pearson's r
slope, intercept = linear_regression(x, y)
NormalDist(mu=100, sigma=15).cdf(130)
Choosing a function
| Function | Returns | |---|---| | mean(data) | the sample arithmetic mean; keeps Fraction and Decimal types | | fmean(data, weights=None) | the mean as a float; runs faster than mean() | | median(data) | the middle value, using the mean of middle two; robust to outliers | | mode(data) | the single most common data point; the first one on ties | | stdev / variance | sample statistics, needing at least two data points | | pstdev / pvariance | population statistics | | quantiles(data, n=4) | n - 1 cut points; n=10 for deciles, n=100 for percentiles |
More
correlation(x, y)returns Pearson's coefficient between -1 and +1;method="ranked"gives Spearman's rank correlation.linear_regression(x, y)returns the slope and intercept estimated by ordinary least squares.NormalDist(mu, sigma)returns an object where mu represents the arithmetic mean and sigma represents the standard deviation.- Empty input raises
StatisticsErrorfor functions such asmean(),fmean()andmedian().
See also
Claims
- id: C1 claim: fmean always returns a float. source: S1 evidence: "This runs faster than the mean() function and it always returns a float ."
- id: C2 claim: median returns the middle value of numeric data. source: S1 evidence: "Return the median (middle value) of numeric data"
- id: C3 claim: statistics raises StatisticsError on bad input. source: S2 evidence: "class StatisticsError(ValueError):"
Sources
- [S1] statistics - Mathematical statistics functions. Python docs. https://docs.python.org/3/library/statistics.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/statistics.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/statistics.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, statistics - Mathematical statistics functions; python/cpython (GitHub), Lib/statistics.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, statistics - Mathematical statistics functions; python/cpython (GitHub), Lib/statistics.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python statistics - mean, median, stdev, quantiles and correlation without NumPy." iinventedlight Library, iil:note:s1f4h8qt, version of 2026-11-18. https://iinventedlight.com/library/notes/python-statistics/. License: CC-BY-4.0.
@misc{iil_note_s1f4h8qt,
author = {{iinventedlight}},
title = {{Python statistics - mean, median, stdev, quantiles and correlation without NumPy}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-statistics/},
note = {Permanent ID iil:note:s1f4h8qt. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-statistics.table.json (table; CC-BY-4.0)
- python-statistics.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-statistics.md.