# Python Program to Find the Variance

Python program to find variance; Through this tutorial, i am going to show you how to find or compute variance in python.

In this tutorial, i will write a programs to compute variance in python.

## Python Program to Find the Variance

See the following python programs to find variance; as shown below:

• Write a program to calculate variance in python.
• Python calculate variance without numpy

### Write a program to calculate variance in python

```import numpy as np
dataset= [21, 11, 19, 18, 29, 46, 20]
variance= np.var(dataset)
print(variance)```

Output

` 108.81632653061224 `

Note:- Python variance() is an inbuilt function that is used to calculate the variance from the sample of data (sample is a subset of populated data). Python statistics module provides potent tools, which can be used to compute anything related to Statistics.

### Python calculate variance without numpy

```#define a function, to calculate variance
def variance(X):
mean = sum(X)/len(X)
tot = 0.0
for x in X:
tot = tot + (x - mean)**2
return tot/len(X)
# call the function with data set
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("variance is: ", variance(x))
y = [1, 2, 3, -4, -5, -6, -7, -8]
print("variance is: ", variance(y))
z = [10, -20, 30, -40, 50, -60, 70, -80]
print("variance is: ", variance(z))```

Output

```variance is:  6.666666666666667
variance is:  16.5
variance is:  2525.0 ```