**Variance:**

In statistics, variance is a key mathematical tool. It is used to handle enormous amounts of data. It is the square of the standard deviation for a given data set.

Variance is also known as the second central moment of a distribution. It is calculated using the mean of the square minus the square of the mean of a particular data set.

Let us calculate the variance by the following methods. They are

- Using Rudimentary Method.
- Using Statistics Module

**Using Rudimentary Method:**

This is the most fundamental method for calculating the variance of lists. In this, we use the mathematical formula to compute the mean and then the variance.

Here, We are not utilizing any of the built-in techniques here, but rather manually computing the variance of lists by writing out the formula.

**Using Statistics Module:**

Python statistics.variance() Method with Examples

**Examples:**

**Example1:**

**Input:**

Given List = [10, 11, 12, 13, 14]

**Output:**

The Given list's Variance = 2.0

**Example2:**

**Input:**

Given List = [12, 6, 5, 2, 1]

**Output:**

The Given list's Variance = 14.959999999999999

## Program for Calculating Variance in Python

### Method #1: Using Built-in Functions (Static Input)

**Using Rudimentary Method:**

**Approach:**

- Give the list as static input and store it in a variable.
- Calculate the sum of all the elements of the given list using the sum() function and store it in a variable.
- Calculate the length of the given list using the len() function and store it in another variable.
- Divide the above-obtained list sum by the length of the given list to get the mean of the list items.
- Store it in another variable.
- Calculate the variance of the given list using the mathematical formula and list comprehension.
- Store it in another variable.
- Print the variance of the given list.
- The Exit of the Program.

**Below is the implementation:**

# Give the list as static input and store it in a variable. gvn_lst = [10, 11, 12, 13, 14] # Calculate the sum of all the elements of the given list using the sum() # function and store it in a variable. lst_sum = sum(gvn_lst) # Calculate the length of the given list using the len() function and store it in # another variable. lst_len = len(gvn_lst) # Divide the above-obtained list sum by the length of the given list to get # the mean of the list items. # Store it in another variable. rslt_mean = lst_sum / lst_len # Calculate the variance of the given list using the mathematical formula and # list comprehension. # Store it in another variable. rslt_varince = sum((a - rslt_mean) ** 2 for a in gvn_lst) / lst_len # Print the variance of the given list. print("The Given list's Variance = ", rslt_varince)

**Output:**

The Given list's Variance = 2.0

### Method #2: Using Built-in Functions (User Input)

**Using Rudimentary Method:**

**Approach:**

- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Calculate the sum of all the elements of the given list using the sum() function and store it in a variable.
- Calculate the length of the given list using the len() function and store it in another variable.
- Divide the above-obtained list sum by the length of the given list to get the mean of the list items.
- Store it in another variable.
- Calculate the variance of the given list using the mathematical formula and list comprehension.
- Store it in another variable.
- Print the variance of the given list.
- The Exit of the Program.

**Below is the implementation:**

# Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_lst = list(map(float, input( 'Enter some random List Elements separated by spaces = ').split())) # Calculate the sum of all the elements of the given list using the sum() # function and store it in a variable. lst_sum = sum(gvn_lst) # Calculate the length of the given list using the len() function and store it in # another variable. lst_len = len(gvn_lst) # Divide the above-obtained list sum by the length of the given list to get # the mean of the list items. # Store it in another variable. rslt_mean = lst_sum / lst_len # Calculate the variance of the given list using the mathematical formula and # list comprehension. # Store it in another variable. rslt_varince = sum((a - rslt_mean) ** 2 for a in gvn_lst) / lst_len # Print the variance of the given list. print("The Given list's Variance = ", rslt_varince)

**Output:**

Enter some random List Elements separated by spaces = 12 6 5 2 1 The Given list's Variance = 14.959999999999999