Python · Module 5

Organising Code7 lessons and their Study toolkit.

Watch each lesson, answer its quiz, then do its practice. After the last lesson: the module quiz, the project, and the interview questions.

7of 7 lessons ready
50quiz questions
32key terms
2self-checking notebooks

The lessonsin the order to take them.

7 lessons ready to study
  1. 5.1Modules and PackagesSplit code across files and import it; use other people's packages. Open
  2. 5.2uv, Packages and EnvironmentsInstall Python and packages for a project without breaking other projects. Open
  3. 5.3Working with FilesRead and write text, CSV and JSON files. Open
  4. 5.4ExceptionsHandle errors deliberately instead of crashing. Open
  5. 5.5DebuggingFind a bug systematically. Open
  6. 5.6Testing with assert and pytestCheck that code does what it should, automatically and every time it changes. Open
  7. 5.7Formatting, Linting and Type CheckingLet tools find the mistakes a reader would otherwise have to find. Open

Glossaryevery term the module introduces.

Each with the lesson that introduces it.

__name__
A module's name; "__main__" for the file being run. if __name__ == "__main__": guards the lines that should run only when the file is run. 5.1
import
Running a module once and binding a name to it. 5.1
module
A Python file that other code imports. 5.1
package
A folder of modules, imported with dotted names: review_stats.text_tools. 5.1
standard library
The modules that come with Python: math, pathlib, csv, json, importlib. 5.1
dependency
A package a project needs, listed in pyproject.toml. 5.2
lockfile
A file recording the exact version of every dependency: uv.lock. 5.2
project
A folder of code with a pyproject.toml that describes it. 5.2
terminal
A window where you type commands for the computer, one line at a time: Terminal on macOS, PowerShell on Windows. 5.2
virtual environment
A folder with its own Python and its own installed packages, for one project: .venv. 5.2
CSV
A table stored as text, one row per line, values separated by commas. A value with a comma in it is written in quotes. 5.3
encoding
The rule that turns text into bytes, and bytes back into text. This course always uses UTF-8. 5.3
file object
What open gives back; you read from it or write to it. with closes it. 5.3
JSON
A text format for dictionaries, lists, strings and numbers. It has no tuples. 5.3
path
The folders and the file name that locate a file: Path("data") / "notes.txt". 5.3
exception
An object Python raises when a line can not be carried out; unhandled, it stops the program. 5.4
handle
Catch an exception with except, so the program carries on. 5.4
raise
Create an exception and stop the function with it: raise ValueError(...). 5.4
traceback
The report Python prints for an unhandled exception: the calls that led to it, and the error itself on the last line. 5.4
breakpoint
The line where the debugger stops. breakpoint() in the code sets one; so does a click beside a line number in VS Code. 5.5
bug
Code that runs, and does something other than what it should. 5.5
debugger
A tool that stops a program so you can look at its values. Python's is pdb; VS Code has one built in. 5.5
reproduce
Make a bug happen on purpose, on an input you can give again. 5.5
assertion
A statement that a condition is True; assert raises AssertionError when it is not. 5.6
edge case
An input at the limit of what the code handles: an empty list, one item, an empty string. 5.6
error case
An input the code should refuse, checked by testing that it raises. 5.6
pytest
A tool that finds the test functions in a project and runs them all. 5.6
regression test
A test written from a bug, so that the bug can not come back unnoticed. 5.6
test
A function that runs code and asserts what it should give. 5.6
formatter
A tool that rewrites code's layout, without changing what it does: ruff format. 5.7
linter
A tool that reads code for mistakes, without running it: ruff check. 5.7
type checker
A tool that checks every call and return against the type hints, without running the code: mypy. 5.7

Module quiz15 questions across it all.

Take it after the last lesson. Your first pick on each question is the one that counts.

Project: review-stats, Finishedbuild it without a template.

Stated as a problem, with no step-by-step instructions. Working out the steps is the point.

The task

Finish review-stats so that:

  1. uv run review-stats data/reviews.csv reads the CSV with csv.DictReader and encoding="utf-8";
  2. it skips any row whose rating is not a whole number, handling the ValueError, and says how many rows it skipped;
  3. it prints the average rating of the rows it kept, and the five most common words without the stop words, using text_tools;
  4. it writes the word counts to results/counts.json with json.dump;
  5. uv run pytest passes, including a test for the skipped row and 5.6's six tests;
  6. uv run ruff check and uv run mypy src report nothing.

Notebooksthat check your answers.

Open them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.

  • Notebook 1 · Modules and PackagesLessons 5.1 Courses plan
  • Notebook 2 · Files and ExceptionsLessons 5.3 and 5.4 Courses plan

Referencefor revising and for interviews.

The cheat sheet is one page of the module’s terms, rules and gotchas. The interview questions come with model answers.