Let’s see how to calculate variance in Python.
How to calculate a variance
To calculate the variance of a sample, we need to import the statistics module.
Luckily, there is a dedicated function in the statistics module to calculate variance.
import statistics as s x = [1, 5, 7, 5, 43, 43, 8, 43, 6] variance = s.variance(x) print("Variance equals: " + str(round(variance, 2)))
Check also how to calculate Variance in Excel.