Skip to content
KALANBLON
Show contents

Chapter 2 of 7 · 20 min

Mean, median and extremes

By the end of this chapter, you'll be able to

  • Compute the minimum, maximum, sum and count of a series
  • Compute a mean and a median
  • Explain why an extreme value distorts the mean
  • Choose the right summary for the data

The basic summaries

A series is a list of values of the same kind: the marks of a class, the sales of each day, the temperatures of the week. Python already has everything needed to sum it up:

  • len(series): the count, the number of values;
  • min(series) and max(series): the smallest and the largest value;
  • sum(series): the sum.
marks = [12, 15, 9, 14, 11, 16, 13]

print("count:", len(marks))
print("lowest:", min(marks), "— highest:", max(marks))
print("range:", max(marks) - min(marks))
print("sum:", sum(marks))
count: 7
lowest: 9 — highest: 16
range: 7
sum: 90

The range (maximum minus minimum) measures how spread out the values are.

Mean and median

The mean (or average) is computed by adding up all the values, then dividing by how many there are. The median is the value in the middle when the series is put in order: half the values are below it, the other half above.

import statistics

marks = [12, 15, 9, 14, 11, 16, 13]

mean = sum(marks) / len(marks)
print("mean:", round(mean, 1))
print("in order:", sorted(marks))
print("median:", statistics.median(marks))
mean: 12.9
in order: [9, 11, 12, 13, 14, 15, 16]
median: 13

sorted returns a copy of the list in ascending order. With 7 marks, the median is the 4th one: 13. With an even number of values, there is no single middle value: statistics.median then takes the mean of the two central values.

Half the class got 13 or less, the other half 13 or more.

Step 4 of 4
Text description of the animation
  1. The marks in the order they were entered.
  2. We put them from smallest to largest.
  3. With 7 values, the 4th leaves 3 values on each side.
  4. Half the class got 13 or less, the other half 13 or more.

Mini-lab: the week's weatherPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: a class report

Here are the marks out of 20 for a test. Write a program that prints the count, the mean rounded to one decimal place, the median, the lowest mark, the highest, and the number of pupils who got at least 10.

marks = [8, 14, 11, 17, 9, 12, 15, 10, 6, 13]
Hint 1
len, min, max and statistics.median give most of the results. For the mean: sum(marks) / len(marks), then round(..., 1).
Hint 2
To count marks of at least 10, go through the list with a loop and add 1 to a counter when mark >= 10.
Show the solution and common mistakes
import statistics

marks = [8, 14, 11, 17, 9, 12, 15, 10, 6, 13]

passed = 0
for mark in marks:
    if mark >= 10:
        passed = passed + 1

print("count:", len(marks))
print("mean:", round(sum(marks) / len(marks), 1))
print("median:", statistics.median(marks))
print("from", min(marks), "to", max(marks))
print(passed, "pupils got at least 10")
count: 10
mean: 11.5
median: 11.5
from 6 to 17
7 pupils got at least 10

With 10 marks, the median is the mean of the 5th and 6th in order (11 and 12): 11.5.

Common mistakes:

  • Writing mark > 10 instead of mark >= 10: the pupil who got exactly 10 would not be counted.
  • Computing the median by taking marks[5] without sorting: you get the 6th mark entered, not the middle value.

Summary

  1. 1len, min, max and sum give the count, the extremes and the sum of a series.
  2. 2The mean adds everything up and divides by the count; the median is the middle value of the sorted series.
  3. 3An extreme value pulls the mean, but barely moves the median.
  4. 4A good summary gives at least the median or the mean, along with the minimum and the maximum.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.