Modules and Imports Checkpoint
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Test what you know about imports, your own modules, the main guard and pip. Each problem uses only the standard library, so every program runs as one file.
Pick the right import form
Use import x when you want each name's source to stay clear, from x import y for a few names you use often, and as for long module names.
import mathfrom statistics import meanimport datetime as dt
print(math.floor(4.7))print(mean([70, 80, 90]))print(dt.date(2026, 11, 3).weekday())Example 1 · Statistics for a list of marks
import statistics
marks = [72, 85, 90, 67, 80]print(statistics.mean(marks))print(statistics.median(marks))print(max(marks) - min(marks))78.8 80 23
What this code does
statisticsis a standard library module, so no pip install is needed.meanis the average, andmedianis the middle value of the sorted list.
Example 2 · Overdue invoices
from datetime import date
today = date(2026, 11, 3)due_dates = ["2026-10-28", "2026-11-20"]for text in due_dates: due = date.fromisoformat(text) print(text, (today - due).days)2026-10-28 6 2026-11-20 -17
What this code does
fromisoformatreads text inyear-month-dayform.- A positive number means the invoice is that many days late. A negative one means it is not yet due.
weekday() counts Monday as 0, so 3 November 2026 (a Tuesday) gives 1. Each form creates different names: math, mean and dt.
A module with a main guard
Keep logic in functions and run it from a main() that sits under the guard. Another file can then import the functions safely.
def pass_rate(marks, limit=40): passed = [m for m in marks if m >= limit] return len(passed) / len(marks) * 100
def main(): print(pass_rate([72, 35, 90, 41]))
if __name__ == "__main__": main()Example 3 · A module-style script
def to_celsius(f): return round((f - 32) * 5 / 9, 1)
def main(): for temp in [32, 98.6, 212]: print(temp, to_celsius(temp))
if __name__ == "__main__": main()32 0.0 98.6 37.0 212 100.0
What this code does
- Logic lives in
to_celsius, which another file could import. main()runs only when this file is started directly.
Imported from another file, this module prints nothing. Run directly, it prints 75.0.
Reading an import error
ModuleNotFoundError means nothing on the import path matches. Check the spelling, the folder, and whether the package needs python -m pip install.
try: import pandas_not_hereexcept ImportError as err: print(f"Cannot import: {err.name}")Catching ImportError is useful for optional tools. Never hide a missing required package without telling the user.
Finish up
Mark complete & continue
Next: Reading and Writing Text Files