Python statistics.variance() Method

Last Updated : 22 Aug 2026

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.

What is the statistics.variance() Method?

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:

Python statistics.variance() Method

Where,

  • S2 = Variance
  • xi = The value of one element.
  • i = Index of elements
  • x ̅ = The mean value of a sample of data.
  • N = The number of elements.

Syntax

It has the following syntax:

statistics.variance(data, xbar)

 

Parameters

The following parameter is used by the variance() method:

  • data: It is a sequence of a list, tuple, or any iterable values that contains numeric values.
  • xbar: An optional parameter that specifies the dataset's mean. If it is excluded or set to None, Python automatically calculates the mean before computing the sample variance.

Return Value

It returns a float value representing the sample variance of the given dataset.

Examples of the statistics.variance() Method

Let’s take several examples that demonstrate the working the statistics.variance() method.

Example 1: Variance with Different Data Types

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))
Execute Now

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.

Example 2: Utilizing Optional xbar Parameter

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))
Execute Now

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.

Example 3: Demonstrating Empty Dataset Error

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))
Execute Now

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.