Arithmetic, Geometric, and Harmonic Means in Python


Python tip:

Use statistics.mean to calculate the arithmetic mean of an iterable.

Use statistics.geometric_mean to calculate the geometric mean of an iterable (Python >= 3.8).

Use statistics.harmonic_mean to calculate the harmonic mean of an iterable.

An example👇

from statistics import geometric_mean, harmonic_mean, mean

print(mean([1, 3, 7, 2, 5]))
# => 3.6

print(geometric_mean([1, 3, 7, 2, 5]))
# => 2.913693458576192

print(harmonic_mean([1, 3, 7, 2, 5]))
# => 2.297592997811816