Python 3.4+statisticsfunction
statistics.pvariance
spread
Population variance (with n).
Syntax
statistics.pvariance(data)
The return type follows the input type: Decimal and Fraction stay exact, and integer data can give you an int rather than a float — do not assume a float comes back. And this is the population variance (divisor n): for a sample use variance with its n-1, and pass mu only when the true mean is genuinely known, otherwise you get the second moment about some unrelated point.
Examples
import statistics
print(statistics.pvariance([1, 2, 3, 4, 5])) # → 2
print(statistics.pvariance([1, 2, 3, 4, 5], mu=0)) # → 11
print(statistics.pvariance([2, 4, 4, 4, 5, 5, 7, 9])) # → 4
print(statistics.pvariance([5])) # → 0
print(statistics.variance([1, 2, 3, 4, 5])) # → 2.5
See also
Open in the full glossarydocs.python.orgReport an inaccuracy