← Back to Python Course | Chapter 13: Data Science & Web | Lesson 7 of 14

Python Statistics Module

The statistics module works out things like the average and the most common value, like a teacher summarizing a class's test scores. It saves you doing the math by hand.
Syntax
python
import statistics

statistics.mean(data)
statistics.median(data)
statistics.stdev(data)

Central Tendency

The built-in statistics module offers simple, dependency-free functions for common calculations: statistics.mean() computes the arithmetic average and statistics.median() finds the middle value of a sorted dataset, which is more robust to outliers than the mean.

Example: Central Tendency

python
import statistics
data = [1, 2, 3, 4, 100]
print(statistics.mean(data))  # arithmetic average, pulled up by the outlier
print(statistics.median(data))  # middle value, unaffected by the outlier

Mode and Frequency

statistics.mode() returns the single most frequently occurring value in a dataset, while statistics.multimode() returns all values tied for the most frequent when there's more than one.

Mode is the only measure of central tendency that also makes sense for non-numeric, categorical data.

Example: Mode and Frequency

python
import statistics
data = [1, 2, 2, 3]
print(statistics.mode(data))  # the single most frequent value
print(statistics.multimode([1, 1, 2, 2, 3]))  # all values tied for most frequent

Dispersion Measures

statistics.variance() and statistics.stdev() quantify how spread out a dataset's values are around the mean -- a small standard deviation means values cluster tightly, a large one means they're widely scattered.

Both come in population and sample variants (pvariance/pstdev vs variance/stdev), and picking the wrong one skews results for small datasets.

Example: Dispersion Measures

python
import statistics
data = [2, 4, 4, 4, 5, 5, 7, 9]
print(statistics.variance(data))  # sample variance
print(statistics.stdev(data))  # sample standard deviation

Data Distribution Ranges

statistics.median_low() and statistics.median_high() resolve the ambiguity that arises when a dataset has an even number of values and there's no single middle element -- they return the lower and upper of the two central values respectively, instead of averaging them.

Example: Data Distribution Ranges

python
import statistics
data = [1, 2, 3, 4]
print(statistics.median_low(data))  # lower of the two middle values
print(statistics.median_high(data))  # upper of the two middle values

Harmonic and Geometric Means

statistics.geometric_mean() and statistics.harmonic_mean() are specialized averages suited to specific situations: geometric mean is appropriate for averaging rates of growth or ratios, and harmonic mean is appropriate for averaging rates like speed, where the arithmetic mean would give a misleading answer.

Example: Harmonic and Geometric Means

python
import statistics
data = [2, 8]
print(statistics.geometric_mean(data))  # suited to averaging rates of growth
print(statistics.harmonic_mean(data))  # suited to averaging rates like speed
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling statistics.mean([]) on an empty list, which raises StatisticsError.
  2. Using statistics.mode() when several values tie, which in older Python versions raises an error; use multimode().
  3. Mixing up population and sample measures, since pstdev() and stdev() give different results.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.