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 .
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 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
- 95% of values fall within
- 99.7% of values fall within
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.