Understand the idea
A problem you can recognise
A revision report may need an average duration, an exact money calculation and a calendar date. Those are different jobs. Treating each as ordinary arithmetic on whatever values happen to be available can create subtle mistakes.
The standard library gives you specialised tools. The useful skill is understanding the contract of the tool you choose, not memorising its import line.
Understand the idea
Understand the idea
The standard library contains focused tools that prevent unnecessary reinvention. Choose a tool by its input and output contract rather than importing a large collection of names and guessing which one might work. statistics.mean computes an arithmetic mean; collections.Counter counts occurrences; itertools can build controlled iteration patterns. Each solves a different problem.
A count is not the same statistic as an average. If duplicate subjects are meaningful events, converting to a set before counting destroys useful frequency information. Likewise, an average can hide variation: two students with very different scores may have the same mean as a consistent student. Report a measure only when it answers the question you are asking.
Inspect edge cases before relying on an operation. An empty data collection may require an explicit decision instead of a default that looks like a valid measurement. Iterators can also be consumed; converting a tiny iterator to a list is useful for inspection, but building enormous collections defeats their purpose. These examples are deliberately bounded and deterministic, requiring no external packages or network services.
See how it works
Example 1: Calculate an arithmetic mean
PYTHON exampleRead the code, then try a change.
Try this example
from statistics import mean
minutes = [20, 30, 40]
print(mean(minutes))
Expected output:
ReferenceCompare this with the explanation.
30
The sum of the three durations is ninety and the item count is three. mean computes their arithmetic average. This does not imply that each session lasted thirty minutes.
See how it works
Example 2: Count repeated values
PYTHON exampleRead the code, then try a change.
Try this example
from collections import Counter
counts = Counter(["Python", "HTML", "Python"])
print(counts["Python"])
print(counts["CSS"])
Expected output:
ReferenceCompare this with the explanation.
2
0
Counter retains frequency rather than discarding repeats. Its missing-member count is zero, a deliberate contract that differs from ordinary dictionary bracket lookup.
See how it works
Example 3: Pair consecutive values
PYTHON exampleRead the code, then try a change.
Try this example
from itertools import pairwise
for previous, current in pairwise([10, 15, 25]):
print(current - previous)
Expected output:
ReferenceCompare this with the explanation.
5
10
pairwise yields adjacent pairs. The loop calculates successive changes without treating the first item as a change from an invented zero. It requires Python 3.10 or newer, supported by this course runtime.
Understand the idea
Think through the important decisions
For an average, inspect both the values and the number of entries. An empty collection does not have an ordinary arithmetic mean to report. Decide how your application should represent that case before assuming every report can always print a useful average.
For exact decimal quantities, pay attention to the construction of Decimal values. Creating a Decimal from suitable text preserves the intended decimal representation. Starting with a binary floating-point approximation and converting it afterwards does not travel backwards and remove the earlier approximation.
For dates, distinguish a calendar date from a duration and from a formatted label. A date tool understands calendar rules that simple arithmetic on a day number does not. Work through a boundary such as moving beyond the end of a month before trusting your own string-based approach.
Check your understanding
Check the reasoning
Choose a standard-library tool according to its input/output contract and preserve meaningful data.
A matching screenshot or output is one check, not proof that a program is correct for all inputs. Change one input, predict the new result and run again. If the result differs, find the first instruction where your prediction stops matching the program state. Keep that small case as a regression check when you fix the cause.
Spot the mistake
The mistake worth understanding
A specialised type does not make every use correct. Read its input requirements and think about boundary cases. Exact arithmetic cannot repair an incorrect formula, and a well-formatted date string is not proof that the underlying date calculation was valid.
Your turn
Independent practice
For durations [20, 30, 20, 40], calculate the mean and count how often 20 occurs. Explain why using a set first would change the meaning of the average and lose frequency information.
Your turn
Practice hint
Pass the original duration list to both tools. Choose mean and Counter for their different purposes.
Change it and explain it
Experiment with the first program For durations [20, 30, 20, 40], calculate the mean and count how often 20 occurs. Explain why using a set first would change the meaning of the average and lose frequency information. Start by changing one input in this smaller example. Loading it does not execute it.
from statistics import mean
minutes = [20, 30, 40]
print(mean(minutes)) Open in Practice Loading an example does not run it. Your existing edits are protected by a replacement confirmation.
Your turn
Try a different case
Explain why each worked example uses its chosen library rather than just ordinary strings or floats. For each one, propose a boundary case that would reveal whether you understand the tool's actual behaviour.