All practice sets

Statistics & coding · Beginning Python

Python: reason about your data

Trace code, handle missing values explicitly, and check a data summary before trusting it.

4 questions · 25–35 minutes

Before you begin: Python lists, loops, conditionals, and functions. A Python interpreter is optional; code is not executed on this page.

Work on paper or use the notes boxes. Numeric checks assess only the labeled value; compare your full reasoning with the solution. Nothing is submitted or saved.

01

Trace before running

What does this code print? Explain which values are excluded.
scores = [72, 85, None, 91, 0]
valid = [x for x in scores if x is not None]
print(len(valid), sum(valid))

Need a hint?

Zero is a valid number. None is the missing-value marker in this example.

Worked solution & reasoning

It prints 4 248. Only None is excluded; 0 remains.

A filter such as “if x” would also remove zero because zero is falsy in Python. That would change the sample and bias the mean upward for these data.

02

Repair a mean function

This function can produce the wrong answer. Rewrite it to exclude None but keep zero, and return None if no usable values remain.
def mean_score(values):
    clean = [x for x in values if x]
    return sum(clean) / len(values)
Need a hint?

Fix both the filter and the empty-list case. Assume all non-None inputs are numbers.

Worked solution & reasoning
def mean_score(values):
    clean = [x for x in values if x is not None]
    if not clean:
        return None
    return sum(clean) / len(clean)

For [72, 85, None, 91, 0], the mean is 62. Returning None for an empty usable sample distinguishes an undefined mean from an actual mean of zero.

03

Design useful checks

Using the corrected mean_score function, write assertions for [0, 10], [None, 10, 20], and [None, None]. Explain which bug each test guards against.
Need a hint?

Include a valid zero, a mix of numbers and missing values, and no usable numbers.

Worked solution & reasoning
assert mean_score([0, 10]) == 5
assert mean_score([None, 10, 20]) == 15
assert mean_score([None, None]) is None

The first detects accidental removal of zero. The second checks missing-value exclusion and the denominator. The third guards against division by zero when no observations remain.

04

Avoid the mean-of-means trap

Class A has 2 students with mean score 90. Class B has 8 students with mean score 60. A script averages the class means and reports 75. Find the overall student mean and explain the error.

Need a hint?

Weight each class mean by the number of students it represents.

Worked solution & reasoning

The total score is 2·90 + 8·60 = 660 across 10 students. The student mean is 66.

The value 75 weights each class equally. That answers a different question. To average across students, use counts as weights, or compute the mean from the individual records.

overall = (2 * 90 + 8 * 60) / (2 + 8)

Review your work

Choose one question you can now explain and one you would like to revisit.