Python 3.4+statisticsfunction
statistics.variance
spread
Sample variance (the square of stdev, with n−1).
Syntax
statistics.variance(data)
The xbar argument is taken on trust: hand it a value that is not the actual mean and you get a number that is no longer a variance, with no error raised. Use pvariance when the data is a whole population rather than a sample, and note that if you only need to compare spreads the square root is redundant — variance and stdev rank datasets identically.
Examples
import statistics
print(statistics.variance([1, 2, 3, 4, 5])) # → 2.5
print(statistics.variance([1, 2, 3, 4, 5], xbar=3)) # → 2.5
print(statistics.variance([2, 4, 4, 4, 5, 5, 7, 9])) # → 4.571428571428571
print(statistics.variance([5])) # → StatisticsError
print(statistics.pvariance([1, 2, 3, 4, 5])) # → 2
See also
Open in the full glossarydocs.python.orgReport an inaccuracy