What is the statistics.pvariance() method in Python?
The statistics.pvariance() method in Python is used to return the population variance of a given data.
This method is unlike the statistics.variance() method, which calculates the variance of a sample of data. Instead, the statistics.pvariance() method calculates the variance of an entire population.
Variance is the sum of the squared distances of each term distributed from the mean of the data divided by the number of observations. It is given by:
- = Variance
- = The value of one observation
- = The mean value of the data
- = The number of observations
Syntax
statistics.pvariance(data, xbar)
Parameters
Parameter | Value |
| This is required. It is the data to be used. It can be a list, a sequence, or iterator |
| This is optional. It is the mean of the data provided. If the value is not provided Python calculates the mean of the data by default. |
Return value
statistics.pvariance() method returns a float value that represents the population variance of a given data.
Example
Let’s use the statistics.pvariance() method to calculate the population variance for a given data.
import statistics# creating a data setdata = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]# to obtain the variancethe_pvariance = statistics.pvariance(data)print('The population variance is', the_pvariance)
Explanation
- Line 1: We import the
statisticsmodule. - Line 4: We create a data set called
data. - Line 6: We use the
statistics.pvariance()method to obtain the population variance of thedataand assign the value to another variablethe_pvariance. - Line 7: We print
the_pvariance, which contains the population variance of the given data.