Source: https://mokxi.com/teachers/plans/python-real-software
Updated: 2026-11-11

Lesson plan

# Python with real habits lesson plan

Advanced Python on a Raspberry Pi, after Python from zero: modules, classes, exceptions, files and logging, tests with plain assert, generators, and a sensor logger that uses them all.

4 chapters, 7 lessons, about 6 hours, on the Raspberry Pi 4 Model B. Every lesson runs in the browser, with nothing to install.

Start a class from this course

The unit

## Objectives and materials

### By the end, students can

- Split code into modules, and bundle data with its functions in classes.
- Exceptions, files and logs: code that survives a loose wire and tells you what happened.
- Tests with assert, and generators that stream data forever.
- A sensor logger with a screen, a CSV and a log, built from every habit in the course.

### Materials for the whole unit

- Eight-channel ADC, MCP3008
- Half breadboard
- LED
- OLED display, 128x64
- Potentiometer
- Pressure, humidity and temperature, BME280
- Raspberry Pi 4 Model B
- Raspberry Pi 5
- Raspberry Pi Zero 2 W
- Resistor

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 40 min

## Structure

Split code into modules, and bundle data with its functions in classes.

Lesson | Time | What students do

Modules Split a program into files: the OLED driver you have been importing all along, the standard library, a module of your own in a new tab, and what __name__ == "__main__" is for. | 50 min

- Run it: a module you did not write
- The standard library
- Write your own module
- Use it on the screen
- Who is __main__?
- Where Python looks

Classes Classes and objects on a BME280: the driver class you already use, a Reading class of your own with methods and __str__, an object that remembers the highest temperature, and inheritance. | 50 min

- Run it: an object from a class
- Write a class
- Teach it to print itself
- An object that remembers
- Inheritance: share what is the same
- When to write a class

### Materials

- OLED display, 128x64
- Pressure, humidity and temperature, BME280
- Raspberry Pi 4 Model B
- Raspberry Pi Zero 2 W

### Check for understanding

Each student finishes "Classes" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "When to write a class".

Chapter 2. About 1 h 40 min

## When things go wrong

Exceptions, files and logs: code that survives a loose wire and tells you what happened.

Lesson | Time | What students do

Exceptions Read a traceback, catch the I2C error a missing chip really raises, scan two addresses without crashing, raise an exception of your own, and clean up with else and finally. | 50 min

- Run it: a crash, on purpose
- Catch it
- Try each address
- Raise your own
- else and finally
- Catch narrow, crash loud

Files and logging Write a knob's readings to a file and read them back, a CSV any spreadsheet opens, the logging module with its levels in place of print, and a log file: what the Pi's engine supports, and what lasts. | 50 min

- Run it: write a file
- Read it back
- CSV: rows and columns
- logging instead of print
- Log to a file
- What lasts, and what does not

### Materials

- Eight-channel ADC, MCP3008
- Half breadboard
- LED
- Potentiometer
- Pressure, humidity and temperature, BME280
- Raspberry Pi 5
- Raspberry Pi Zero 2 W
- Resistor

### Check for understanding

Each student finishes "Files and logging" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "What lasts, and what does not".

Chapter 3. About 1 h 40 min

## Craft

Tests with assert, and generators that stream data forever.

Lesson | Time | What students do

Tests with assert No pip and no pytest, so tests are plain assert: catch a real off-by-one in a voltage conversion, test the edges, write a ten-line test runner, and let a failing test drive a fix. | 50 min

- Run it: a test that fails
- Fix the bug, watch it pass
- Test the edges
- A test runner in ten lines
- A failing test drives a fix
- Habits worth keeping

Generators yield and next, a generator that streams knob readings forever, a moving average built as a pipeline of generators, islice to take just a few, and generator expressions. | 50 min

- Run it: yield
- next, and the end
- A stream that never ends
- A pipeline: generators feeding generators
- Take a few, and generator expressions
- Why generators

### Materials

- Eight-channel ADC, MCP3008
- Half breadboard
- LED
- Potentiometer
- Raspberry Pi 5
- Resistor

### Check for understanding

Each student finishes "Generators" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Why generators".

Chapter 4. About 1 h

## Boss project

A sensor logger with a screen, a CSV and a log, built from every habit in the course.

Lesson | Time | What students do

Project: a sensor logger The boss project: a BME280 and an OLED on one I2C bus, a logger module with a Sample class and a generator of samples, a test before anything is trusted, a CSV log, the latest reading on the screen and a warning when it gets hot. | 60 min

- Run it: two chips, one bus
- A module: Sample and samples()
- Test before you trust
- Log to CSV
- Show it, and warn when hot
- The screen shows the latest
- What you built

### Materials

- OLED display, 128x64
- Pressure, humidity and temperature, BME280
- Raspberry Pi Zero 2 W

### Check for understanding

Each student finishes "Project: a sensor logger" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "What you built".

Your class

## Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.

Start a class from this course See the demo class

More units

## Other lesson plans

Electronics basics 20 lessons, about 10 h. Embedded on the Arduino Uno 16 lessons, about 12 h. Embedded on the ESP32-C3 13 lessons, about 11 h. Embedded on the Raspberry Pi Pico 7 lessons, about 6 h. Build your own fight stick 5 lessons, about 4 h. Embedded on the STM32F411 6 lessons, about 6 h. C++ on a board 13 lessons, about 10 h. Digital logic and memory 4 lessons, about 4 h. Interactive devices on the Uno 9 lessons, about 8 h. From gates to a computer 23 lessons, about 16 h. Build a claw machine 8 lessons, about 6 h. Every unit All the courses as ready-made units, on the teachers page.
