If you need to calculate variance in Python, then you can do the following.

Option 1 – Using variance() from Statistics module

1
2
3
4
5
6
7
import statistics

list = [12,14,10,6,23,31]
print("List : " + str(list))

var = statistics.variance(list)
print("Variance: " + str(var))

Option 2 – Using var() from numpy module

1
2
3
4
5
6
import numpy as np

arr = [12,43,24,17,32]

print("Array : ", arr)
print("Variance: ", np.var(arr))

Option 3 – Using sum() and List Comprehensions

1
2
3
4
list = [12,43,24,17,32]
average = sum(list) / len(list)
var = sum((x-average)**2 for x in list) / len(list)
print(var)