Lesson 2 of 18

Variance & Standard Deviation

Spread of Data

Variance and standard deviation measure how spread out data is around the mean.

The sample variance is:

s^2 = rac{1}{n-1}sum_{i=1}^n (x_i - ar{x})^2

The sample standard deviation is s=sqrts2s = sqrt{s^2}.

import statistics

data = [1, 2, 3, 4, 5]

var = statistics.variance(data)   # sample variance (ddof=1)
std = statistics.stdev(data)      # sample std deviation

print(round(var, 2))   # 2.5
print(round(std, 2))   # 1.58

Population vs Sample

Use statistics.pvariance / statistics.pstdev for the population — when you have all the data. Use statistics.variance / statistics.stdev for a sample — when your data is a subset of a larger population. This gives an unbiased estimate (ddof=1).

Standard Deviation

The standard deviation sigmasigma is the square root of variance — it has the same units as the data, making it easier to interpret.

For normally distributed data, approximately:

  • 68% of values fall within pm1sigmapm 1sigma
  • 95% of values fall within pm2sigmapm 2sigma
  • 99.7% of values fall within pm3sigmapm 3sigma

Your Task

Implement spread(data) that prints the sample variance (rounded to 2 decimal places) and sample standard deviation (rounded to 2 decimal places).

Pyodide loading...
Loading...
Click "Run" to execute your code.