← Back to Blog

statistics — What Your Data Is Telling You

The idea

You have a list of numbers. Scores, prices, temperatures, times.

Raw numbers tell you something. But averages, medians, and spreads tell you more.

Python's statistics module does the math — no formula needed.

Importing

import statistics

The center — mean, median, mode

import statistics

scores = [85, 74, 91, 63, 98, 74, 85]

print(statistics.mean(scores))      # 81.43 — average
print(statistics.median(scores))    # 85    — middle value
print(statistics.mode(scores))      # 74    — most frequent

mean — sum divided by count. Sensitive to extreme values.

median — the middle value when sorted. Not affected by extremes.

mode — the most frequent value. Raises StatisticsError if no value appears more than once.

multimode() — when there's more than one mode

scores = [85, 74, 91, 85, 74]
print(statistics.multimode(scores))

Output → [85, 74]

Returns all values that appear most frequently. Never raises an error.

The spread — stdev and variance

How spread out are the values?

scores = [85, 74, 91, 63, 98]

print(statistics.stdev(scores))     # 13.28 — standard deviation
print(statistics.variance(scores))  # 176.3 — variance

stdev (standard deviation) — how far values typically are from the mean. Low = clustered. High = spread out.

variance — stdev squared. Less intuitive, but used in calculations.

A practical example

import statistics

squad_scores = [85, 74, 91, 63, 98, 77, 88]

print(f"Mean:   {statistics.mean(squad_scores):.1f}")
print(f"Median: {statistics.median(squad_scores)}")
print(f"Stdev:  {statistics.stdev(squad_scores):.1f}")
print(f"Min:    {min(squad_scores)}")
print(f"Max:    {max(squad_scores)}")

Output:

Mean:   82.3
Median: 85
Stdev:  11.8
Min:    63
Max:    98

Five lines. A complete picture of the data.

Heads up!

  • mode() raises StatisticsError if all values are unique — use multimode() to be safe
  • mean() returns a float — median() may return an int or float depending on the list
  • These functions work on any list of numbers — integers or floats
  • min() and max() are built-ins — no import needed

What you should understand now

  • mean() — average
  • median() — middle value
  • mode() — most frequent value
  • multimode() — all most frequent values, no error
  • stdev() — how spread out the values are
[ login to bookmark ] // copied! 18 views · 1 min
// resources
Code Example module_statistics.py
← prev Dates, Times, and Timestamps in Python next → Timing Your Code with the time Module
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.