pvariance & variance Functions of statistics Module in Python (2 Examples)
In this Python tutorial you’ll learn how to apply the pvariance and variance functions of the statistics module.
The content looks as follows:
So now the part you have been waiting for – the examples!
Introduction of Exemplifying Data
We use the following data as a basis for this Python programming tutorial:
my_list = [4, 1, 2, 8, 2, 2, 5, 1, 8, 9, 7, 9, 1, 3] # Create example list print(my_list) # Print example list # [4, 1, 2, 8, 2, 2, 5, 1, 8, 9, 7, 9, 1, 3]
As you can see based on the previous Python console output, our exemplifying data is a list containing different integer elements.
Example 1: Get Population Variance Using pvariance() Function of statistics Module
In Example 1, I’ll show how to calculate the population variance using the pvariance function of the statistics module in Python.
We first have to load the statistics module to Python, if we want to use the functions that are contained in the library:
import statistics # Import statistics
Next, we can apply the statistics.pvariance function to our data to get the population variance:
print(statistics.pvariance(my_list)) # Apply pvariance() function # 9.244897959183673
The previous console output shows our result, i.e. the population variance of our list is 9.24.
Example 2: Get Sample Variance Using variance() Function of statistics Module
This example shows how to get the sample variance using the variance function of the statistics module.
For this, we can apply the variance function as shown below:
print(statistics.variance(my_list)) # Apply variance() function # 9.956043956043956
The previous output of the Python console shows the sample variance of our list object.
Video & Further Resources
Have a look at the following video on the Statistics Globe YouTube channel. In the video tutorial, I’m explaining the content of this article.
The YouTube video will be added soon.
In addition, you might read the related tutorials on this homepage:
- Variance in Python
- Variance by Group in Python
- Variance of NumPy Array in Python
- stdev & pstdev Functions of statistics Module
- All Python Programming Examples
In this Python tutorial you have learned how to use the pvariance and variance functions of the statistics module. In case you have any further questions, let me know in the comments.