In Python, variance() is a statistical function used to calculate the variance from a sample of data (from a population).
In this chapter, we will discuss the statistics.variance() method with its syntax, parameters, return value, and several examples.
The statistics.variance() function in Python is used to calculate the sample variance of a dataset. Sample variance measures how far the data values are spread from the sample mean. It is commonly used when the data represents only a sample of an entire population. A larger variance indicates greater variation among the values, while a smaller variance means the values are closer to the mean. It is widely used in statistics and data analysis to measure the variability of sample data.
Variance is the sum of the squared distances of each term distributed from the mean of the data divided by the number of observations.
The mathematical formula used for calculating variance is:

Where,
It has the following syntax:
statistics.variance(data, xbar)
The following parameter is used by the variance() method:
It returns a float value representing the sample variance of the given dataset.
Let’s take several examples that demonstrate the working the statistics.variance() method.
The following example demonstrates how the variance() method works with different types of datasets.
Python
# Import required modules import statistics from fractions import Fraction as frac ## Define datasets int_dataset = [6, 5, 8, 9, 5, 3, 6, 5] float_dataset = (5.5, 3.6, 3.6, 2.2, 5.5) frac_dataset = [frac(5, 7), frac(3,9), frac(7, 9), frac(3, 9)] neg_dataset = [-2, -4, -4, -4, -5, -5, -7, -9] # Finding variance of datasets. print(statistics.variance(int_dataset)) print(statistics.variance(float_dataset)) print(statistics.variance(frac_dataset)) print(statistics.variance(neg_dataset))
Output:
3.5535714285714284 2.0069999999999997 76/1323 4.571428571428571
Explanation
In the given example, we imported the statistics and fractions modules and created four datasets: int_dataset, float_dataset, neg_dataset, and frac_dataset. After that, we passed these datasets to the variance() method to find the variance of each dataset.
The following example demonstrates how the variance() method works with negative elements of a dataset.
Python
# Import statistics module
import statistics
# Define dataset
dataset = (5.5, 3.6, 3.6, 2.2, 5.5)
# First, find the mean of the dataset
meanValue = statistics.mean(dataset)
# Then, find the variance of the dataset.
print("Variance of dataset:", statistics.variance(dataset, xbar=meanValue))
Output:
Variance of dataset: 2.007
Explanation
In the given example, we imported the statistics module and created a dataset. We first calculated the mean of the dataset and then passed it to the statistics.variance() method. The output shows the variance of the dataset.
The following example demonstrates that passing an empty sequence, such as an empty set or list, to the variance() method raises an error.
Python
# Import statistics module import statistics # Define dataset dataset = set() # Demonstrating empty dataset error print(statistics.variance(dataset))
Output:
--------------------------------------------------------------------------- StatisticsError Traceback (most recent call last) /tmp/ipykernel_2330/2667872870.py in| () 6 7 # Demonstrating empty dataset error ----> 8 print(statistics.variance(dataset)) /usr/lib/python3.12/statistics.py in variance(data, xbar) 912 T, ss, c, n = _ss(data, xbar) 913 if n < 2: --> 914 raise StatisticsError('variance requires at least two data points') 915 return _convert(ss / (n - 1), T) 916 StatisticsError: variance requires at least two data points |
Explanation
In the above example, we imported the statistics module and created an empty dataset. When we pass this dataset to the variance() method, it raises a StatisticsError: variance requires at least two data points. It indicates that an empty dataset is not allowed.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India