Python Glossary Русский

Python 3.8+statisticsfunction

statistics.fmean

mean

Fast floating-point arithmetic mean (faster than mean; 3.8+).

Syntax

statistics.fmean(data)

Speed is paid for in precision: fmean converts everything to float, so Decimal and Fraction inputs lose their exact arithmetic, while mean stays exact and keeps the type. The function itself dates to 3.8, but the weights argument only appeared in 3.11 — earlier versions do not accept it.

Examples

# Python 3.11+
import statistics
print(statistics.fmean([1, 2, 3, 4]))   # → 2.5
print(statistics.fmean([4, 5, 3, 5]))   # → 4.25
print(statistics.fmean([80, 90], weights=[1, 3]))   # → 87.5
print(statistics.mean([1, 2, 3]), statistics.fmean([1, 2, 3]))   # → 2 2.0
print(statistics.fmean([]))   # → StatisticsError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy