Python 3.4+statisticsfunction
statistics.mean
mean
Arithmetic mean (average) of the data.
Syntax
statistics.mean(data)
mean is exact (fractions under the hood) and preserves the input type: a list of ints can come back as an int, Fractions stay Fractions, Decimals stay Decimals; when you just want a float and speed, use fmean. It is also dragged by outliers — a single freak value shifts it noticeably while median barely moves. Empty data raises StatisticsError rather than returning 0.
Examples
import statistics
print(statistics.mean([1, 2, 3, 4])) # → 2.5
print(statistics.mean([1, 2, 3])) # → 2
print(round(statistics.mean([4, 5, 3, 5, 4]), 2)) # → 4.2
print(statistics.mean([1, 2, 3, 100]), statistics.median([1, 2, 3, 100])) # → 26.5 2.5
print(statistics.mean([])) # → StatisticsError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy