Descriptive statistics in Python with the statistics module

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Descriptive statistics in Python with the statistics module

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line must come first before you can use statistics.mean() in Python?
A import statistics
B include statistics
2. Which call finds the average of the list marks = [60, 70, 80]?
A statistics.middle(marks)
B statistics.mean(marks)
3. For data = [2, 4, 9, 11, 20], which function returns 9?
A statistics.median(data)
B statistics.mean(data)
4. Which function measures the spread of the values in a list?
A statistics.stdev(data)
B statistics.median(data)
5. Which code finds the range of the list data?
A max(data) + min(data)
B max(data) - min(data)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1statistics.median(marks) Astatistics.stdev(marks)
2max(marks) - min(marks) Bimport statistics print(statistics.mean(marks))
3Make the statistics functions available before calculating the average of a list Cmean() is called without the statistics prefix, so Python raises a NameError. Write statistics.mean(marks) after import statistics.
4Measure the spread of a sample of data using standard deviation DThe subtraction is the wrong way round, which gives a negative range. It should be max(marks) - min(marks).
5print(mean(marks)) EReturns the middle value of the list marks once it is sorted
6range = min(marks) - max(marks) FCalculates the range, the gap between the highest and lowest values

Definitions

statistics.mean(data)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
______ statistics marks = [72, 85, 90, 64] print(statistics.mean(marks))
2.
import statistics marks = [72, 85, 90, 64] print(statistics.____(marks))
3.
import statistics ages = [15, 17, 16, 18, 16] print(statistics.______(ages))
4.
import statistics scores = [4, 8, 6, 5, 7] print(statistics._____(scores))
5.
data = [12, 18, 9, 25, 14] spread = max(data) - ___(data) print(spread)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import statistics
B data = [4, 8, 6, 5, 7
C avg = statistics.mean(data)
D print(avg)
Question 2
A import statistics
B marks = [70, 85, 90, 65]
C mid = statistics.median(marks)
D print("Median:" mid)
Question 3
A import statistics
B marks = [52, 61, 75, 80, 68]
C sd = statistics.stdev(marks)
D print(round(s, 2))
Question 4
A import statistics
B data = [3, 9, 4, 7, 12]
C total = sum[data]
D print(total / len(data))
Question 5
A data = [14, 22, 9, 31, 18]
B biggest = max(data)
C smallest = max(data)
D print(biggest - smallest)

Answer Key

Descriptive statistics in Python with the statistics module
Section A: Quick Questions
Q1
A — import statistics
Q2
B — statistics.mean(marks)
Q3
A — statistics.median(data)
Q4
A — statistics.stdev(data)
Q5
B — max(data) - min(data)
Section B: Match the Pairs
Q1
statistics.median(marks) → Returns the middle value of the list marks once it is sorted
Q2
max(marks) - min(marks) → Calculates the range, the gap between the highest and lowest values
Q3
Make the statistics functions available before calculating the average of a list → import statistics print(statistics.mean(marks))
Q4
Measure the spread of a sample of data using standard deviation → statistics.stdev(marks)
Q5
print(mean(marks)) → mean() is called without the statistics prefix, so Python raises a NameError. Write statistics.mean(marks) after import statistics.
Q6
range = min(marks) - max(marks) → The subtraction is the wrong way round, which gives a negative range. It should be max(marks) - min(marks).
Section C: Definitions
Q1
statistics.mean(data) — The statistics module calculates descriptive statistics on a list: mean() is the average, median() the middle value, stdev() the spread. Use import statistics first.
Section D: Fill in the Blanks
Q1
import statistics marks = [72, 85, 90, 64] print(statistics.mean(marks))
Q2
import statistics marks = [72, 85, 90, 64] print(statistics.mean(marks))
Q3
import statistics ages = [15, 17, 16, 18, 16] print(statistics.median(ages))
Q4
import statistics scores = [4, 8, 6, 5, 7] print(statistics.stdev(scores))
Q5
data = [12, 18, 9, 25, 14] spread = max(data) - min(data) print(spread)
Section E: Spot the Error
Q1
B — The list is missing its closing square bracket. It should be data = [4, 8, 6, 5, 7].
Q2
D — print() needs a comma between its arguments. It should be print("Median:", mid).
Q3
D — The variable is called sd, not s, so Python would raise a NameError. It should be print(round(sd, 2)).
Q4
C — Functions are called with round brackets, not square brackets. It should be total = sum(data).
Q5
C — The smallest value needs min(), not max(). With max() the range would always be 0. It should be smallest = min(data).