# Python Program to Calculate the Variance

Python program to find variance; In this tutorial, you will learn how to find or calculate the variance in python using numpy.

## Python Program to Calculate the Variance

• 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

# 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 ```

## Recommended Python Programs 