Python statistics.variance() method is used to calculate the variance of input data.

Variance quantifies how much the numbers in the dataset diverge from the mean (average) value. It measures the spread of a dataset and is calculated as the average of the squared differences between each data point and the mean of the dataset.

**Syntax**

`statistics.variance(data, xbar=None)`

**Parameters**

**data (required):**It is an array of valid numbers, including Decimal and Fraction values.**xbar (optional):**The mean is automatically calculated if this parameter is not given(None).

**Return value**

It returns the variance of the input data.

**Visual representation**

**Example 1: How to Use statistics.variance() Method**

```
import statistics
input_data = [21, 19, 11, 21, 19, 46, 29]
output = statistics.variance(input_data)
print(output)
```

**Output**

```
124.23809523809524
```

**Example 2: Passing range of data-types**

```
from statistics import variance
from fractions import Fraction as fr
# Dataset of a mix of small and large positive integers
data1 = (3, 7, 11, 18, 20, 25, 30)
# Dataset of negative integers
data2 = (-10, -8, -5, -3, -1)
# Dataset of mixed positive and negative integers
data3 = (-5, -3, 0, 2, 4, 6, 9, 15)
# Dataset of fractional numbers
data4 = (fr(1, 3), fr(1, 4), fr(2, 5), fr(3, 7), fr(4, 9))
# Dataset of floating-point numbers with decimals
data5 = (0.5, 1.2, 1.8, 2.3, 2.7, 3.1)
# Print the variance of each sample
print("Variance of data1 is:", variance(data1))
print("Variance of data2 is:", variance(data2))
print("Variance of data3 is:", variance(data3))
print("Variance of data4 is:", variance(data4))
print("Variance of data5 is:", variance(data5))
```

**Output**

`Variance of data1 is: `**95.23809523809524**
Variance of data2 is: **13.3**
Variance of data3 is: **42.57142857142857**
Variance of data4 is: **50821/7938000**
Variance of data5 is: **0.9386666666666668**

**Example 3: Passing the ‘xbar’ parameter**

The xbar parameter represents the mean of the data, and it’s used when the mean has already been calculated or if you want to compute the variance relative to a specific mean value.

```
import statistics
# Creating a sample list
sample = [1.1, 1.2, 1.9, 2.1, 1.8, 2.2]
# Calculating the mean of sample list
mean = statistics.mean(sample)
# Calculating the variance of sample list
print("Variance of Sample list is % s"
% (statistics.variance(sample, xbar=mean)))
```

**Output**

`Variance of Sample list is `**0.2136666666666667**

**Example 4: statistics.StatisticsError: variance requires at least two data points**

The statistics.StatisticsError: variance requires at least two data points error occurs if you try to calculate the variance of a dataset with fewer than two values. The variance inherently involves the spread or dispersion of the data points, and with less than two values, there’s nothing to compare or spread.

```
import statistics
# creating a sample list
sample = []
print(statistics.variance(sample))
```

**Output**

`statistics.StatisticsError: variance requires at least two data points`

To fix this code, you need to ensure that the sample list contains at least two numeric values.

Here’s a corrected version of the code:

```
import statistics
# creating a sample list with at least two values
sample = [1, 2, 3, 4, 5]
# Calculating and printing the variance
print("Variance of the sample is:", statistics.variance(sample))
```

**Output**

`Variance of the sample is: `**2.5**

That’s it.