statistics.variance() Method in Python:
The statistics.variance() method computes the variance from a data sample (from a population).
A high variance indicates that the data is dispersed, whereas a low variance indicates that the data is tightly clustered around the mean.
Look at the statistics.pvariance() method to calculate the variance of an entire population.
Syntax:
statistics.variance(data, xbar)
Parameters
data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).
xbar: This is Optional. The arithmetic mean of the given data. If omitted (or set to None), the mean is calculated automatically.
Note: It should be noted that if the data has fewer than two values, StatisticsError is returned.
Return Value:
Returns a float value representing the given data’s sample variance.
Examples:
Example1:
Input:
Given list = [10, 20, 40, 15, 30]
Output:
The variance of the given list items [10, 20, 40, 15, 30] = 145
Example2:
Input:
Given list = [3, 2, 5, 6, 1, 1, 3]
Output:
The variance of the given list items [3, 2, 5, 6, 1, 1, 3] = 3.6666666666666665
statistics.variance() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Import statistics module using the import keyword.
- Give the list as static input and store it in a variable.
- Pass the given list as an argument to the statistics.variance() method that computes the variance of the given list items.
- Store it in another variable.
- Print the variance of the given list items.
- The Exit of the Program.
Below is the implementation:
# Import statistics module using the import keyword. import statistics # Give the list as static input and store it in a variable. gvn_lst = [10, 20, 40, 15, 30] # Pass the given list as an argument to the statistics.variance() method that # computes the variance of the given list items. # Store it in another variable. rslt = statistics.variance(gvn_lst) # Print the variance of the given list items. print("The variance of the given list items", gvn_lst, "= ", rslt)
Output:
The variance of the given list items [10, 20, 40, 15, 30] = 145
Method #2: Using Built-in Functions (User Input)
Approach:
- Import statistics module using the import keyword.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in another variable.
- Pass the given list as an argument to the statistics.variance() method that computes the variance of the given list items.
- Store it in another variable.
- Print the variance of the given list items.
- The Exit of the Program.
Below is the implementation:
# Import statistics module using the import keyword. import statistics # Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Pass the given list as an argument to the statistics.variance() method that # computes the variance of the given list items. # Store it in another variable. rslt = statistics.variance(gvn_lst) # Print the variance of the given list items. print("The variance of the given list items", gvn_lst, "= ", rslt)
Output:
Enter some random List Elements separated by spaces = 3 2 5 6 1 1 3 The variance of the given list items [3, 2, 5, 6, 1, 1, 3] = 3.6666666666666665