Python statistics.pvariance() Method with Examples

statistics.pvariance() Method in Python:

The statistics.pvariance() method computes the variance of a whole population.

A high variance indicates that the data is dispersed, whereas a low variance indicates that the data is tightly clustered around the mean.

Examine the statistics.variance() method to determine the variance from a sample of data.

Syntax:

statistics.pvariance(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 (can also be a second moment around a point that is not the mean). If omitted (or set to None), the mean is calculated automatically.

Note: It is important to note that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a  float value representing the given data’s population variance.

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The variance of the given list items [10, 20, 40, 15, 30] =  116

Example2:

Input:

Given list = [1, 6, 9, 7]

Output:

The variance of the given list items [1, 6, 9, 7] = 8.6875

statistics.pvariance() 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.pvariance() method that computes the variance of a whole population.
  • 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.pvariance() method that
# computes the variance of a whole population.
# Store it in another variable.
rslt = statistics.pvariance(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] =  116

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.pvariance() method that computes the variance of a whole population.
  • 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.pvariance() method that
# computes the variance of a whole population.
# Store it in another variable.
rslt = statistics.pvariance(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 = 1 6 9 7
The variance of the given list items [1, 6, 9, 7] = 8.6875