M3: Functions, files, libraries & errors
Until now your code lived in a browser and ran top to bottom. Today you move to your own machine and learn the four habits that turn scripts into real software: wrap code in functions you can reuse, pull in libraries other people wrote, read and write files (including JSON: the text form of M2's dictionaries), and catch errors so a typo doesn't crash everything. This is the exact toolkit the AI API needs in Part B.
Today's win: set up Python on your own computer, then run a program that reads a file, uses a library you installed, organizes its work into functions, saves JSON, and survives a bad line of data, and you understand every piece.
Today you will
- Install Python + a virtual environment + pip on your own machine (your first local install)
- Organize code into functions and pull in an installed library
- Read a file → transform it → save it as JSON, and add
try/exceptso mistakes don't crash you
Run of show (~75 min)
| Time | What we do |
|---|---|
| 0:00 | Hook + the win we're chasing |
| 0:05 | The one idea: reusable code that survives the real world (full read in notes.md) |
| 0:10 | Lab Part A: install Python + venv + a library (go slow, pairs) |
| 0:40 | Lab Part B: functions, read a file, save JSON, handle errors |
| 1:10 | Show: post your rich table + saved .json to the wins board |
| 1:15 | Wrap + take-home |
If you get stuck
- Installing is the hard part, and getting stuck is normal: that's exactly why we go one tiny step at a time, per OS. Nothing here can harm your computer.
- Re-read the You should now see line. Most setup problems are: the
(.venv)prefix is missing (re-activate), or it'spythonvspython3(use whichever your version check accepted). - The full per-OS steps and a troubleshooting table live in
resources/install-guides/python-venv.md. Lean on it.
Instructor note: for a group new to the terminal, run Part A (setup) as its own session and Part B (the code) as the next. The lab is cut at that seam. This is the most setup-heavy module in the course, budget extra time.
Optional challenge
Only if you'll run AI models on your own machine later: skim the PyTorch tooling box in
notes.md and install the CPU-only build using the official selector. You do not
need it for this course, we use hosted APIs, so skip it with a clear conscience otherwise.