Python · Module 5
Organising Code
Take each lesson, answer its quiz, then do its practice. After the last lesson come the module quiz, the project and the interview questions.
Lessons
- 5.1Modules and PackagesSplit code across files and import it; use other people's packages.
- 5.2uv, Packages and EnvironmentsInstall Python and packages for a project without breaking other projects.
- 5.3Working with FilesRead and write text, CSV and JSON files.
- 5.4ExceptionsHandle errors deliberately instead of crashing.
- 5.5DebuggingFind a bug systematically.
- 5.6Testing with assert and pytestCheck that code does what it should, automatically and every time it changes.
- 5.7Formatting, Linting and Type CheckingLet tools find the mistakes a reader would otherwise have to find.
Module material
Take these after the last lesson, in this order. Next on each page leads through them.
- Module quiz15 questions across the whole module. Take it after the last lesson; your first pick on each question is the one that counts.
- Project: review-stats, FinishedStated as a problem, with no template and no step-by-step instructions. Working out the steps is the point.
- NotebooksOpen them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.
- Cheat sheetOne page of the module’s terms, rules and gotchas, with the output each rule produces.
- Interview questions25 questions interviewers ask on organising code, from first principles to the follow-ups. The first 3 model answers are free to read; Pro opens the rest.
Glossary
Every term the module introduces, 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.tomlthat 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
opengives back; you read from it or write to it.withcloses 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;assertraisesAssertionErrorwhen 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