Python Glossary Русский

Python 3.8+statisticsfunction

statistics.geometric_mean

mean

Geometric mean (the n-th root of the product).

Syntax

statistics.geometric_mean(data)

Reach for it when values multiply rather than add: an average growth factor over several periods is a geometric mean, and the arithmetic mean overstates it. A negative value raises StatisticsError, while a zero has quietly returned 0.0 since Python 3.13 (earlier versions raise StatisticsError for it too). The result is a float computed with no exactness guarantees, so don't compare it with ==.

Examples

import statistics
print(round(statistics.geometric_mean([1, 4, 16]), 1))   # → 4.0
print(round(statistics.geometric_mean([3, 12]), 1))   # → 6.0
print(round(statistics.geometric_mean([1.10, 1.21, 1.331]), 2))   # → 1.21
print(statistics.mean([1, 100]), round(statistics.geometric_mean([1, 100]), 1))   # → 50.5 10.0
print(statistics.geometric_mean([-1, 4]))   # → StatisticsError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy