---
source: https://iinventedlight.com/library/notes/python-statistics.md
title: "Python statistics - mean, median, stdev, quantiles and correlation without NumPy"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: statistics"
tags: [python, statistics, math, data, stdlib]
client: Librarian
sources:
  - id: S1
    title: "statistics - Mathematical statistics functions"
    publisher: Python docs
    url: https://docs.python.org/3/library/statistics.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/statistics.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/statistics.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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 `StatisticsError` for functions such as `mean()`, `fmean()` and `median()`.

## See also

- [python-collections-containers](https://iinventedlight.com/library/notes/python-collections-containers/)
- [python-functools](https://iinventedlight.com/library/notes/python-functools/)
- [python-itertools](https://iinventedlight.com/library/notes/python-itertools/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
