Source: https://mokxi.com/teachers/plans/iterators-and-functional-python
Updated: 2026-11-19

Lesson plan

# Iterators and functional Python lesson plan

Comprehensions, the iterator protocol, generators in depth, map and filter, itertools and functools, ending in a streaming sensor pipeline on a Raspberry Pi with an MCP3008.

4 chapters, 7 lessons, about 5 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

- Comprehensions, and what a for loop really does underneath.
- Generators that pause, take values in, hand over and clean up.
- itertools and functools, and what to write when one is missing.
- A knob, an ADC and an LED, joined by a pipeline of generators.

### Materials for the whole unit

- Eight-channel ADC, MCP3008
- Half breadboard
- LED x 2
- Potentiometer
- Raspberry Pi 4 Model B
- Raspberry Pi 5
- Resistor x 2

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 20 min

## Beyond the for loop

Comprehensions, and what a for loop really does underneath.

Lesson | Time | What students do

Comprehensions List comprehensions that filter and transform sonar pings, the if-else expression inside one, dict and set comprehensions over the planets, nested comprehensions that flatten and turn a grid, and generator expressions that light an alarm. | 40 min

- Run it: the loop you already know
- The same list in one line
- Choose a value per item
- Dicts and sets too
- Nested comprehensions
- Generator expressions and an alarm

Iterators iter() and next(), the for loop written out by hand with StopIteration, why zip and map work only once, an iterator class with __iter__ and __next__, iterables that can be looped again, and a countdown that blinks an LED. | 40 min

- Run it: next, next, next
- A for loop, by hand
- One pass only
- Write your own iterator
- Iterables start over
- Count down on a real LED

### Materials

- Half breadboard
- LED x 2
- Raspberry Pi 4 Model B
- Resistor x 2

### Check for understanding

Each student finishes "Iterators" 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: "Count down on a real LED".

Chapter 2. About 1 h 20 min

## Lazy streams

Generators that pause, take values in, hand over and clean up.

Lesson | Time | What students do

Generators in depth What yield really does: lazy start, infinite generators, send() for a running average, yield from with a return value, close() and finally for cleanup, and a pipeline of generator expressions that shows exactly when each value moves. | 40 min

- Run it: a function that pauses
- Forever is fine
- send(): values go in, too
- yield from, and a return value
- close() and finally
- A lazy pipeline

map and filter Functions as values: map() to transform, filter() and lambda to keep, map over two lists at once, a dict of converter functions, key functions for sorted() and max(), and a lazy filter that blinks an LED once per hot reading. | 40 min

- Run it: map
- filter and lambda
- Two lists at once
- Functions in a dict
- Key functions
- Blink once per hot reading

### Materials

- Half breadboard
- LED x 2
- Raspberry Pi 4 Model B
- Resistor x 2

### Check for understanding

Each student finishes "map and filter" 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: "Blink once per hot reading".

Chapter 3. About 1 h 20 min

## The toolkits

itertools and functools, and what to write when one is missing.

Lesson | Time | What students do

itertools The standard iterator toolkit: count and islice, cycle for a watch rota, accumulate and pairwise for running totals and differences, groupby and why it needs to be sorted, combinations and product, and chain, takewhile and dropwhile. | 40 min

- Run it: count and islice
- cycle: a watch rota
- Running totals and differences
- groupby needs sorted data
- Combinations and products
- chain, takewhile, dropwhile

functools reduce and the operator module, partial to pre-fill arguments for an ADC conversion, lru_cache turning 21,891 calls into 21, decorators with wraps and what this engine does differently, and a compose() of your own. | 40 min

- Run it: reduce
- reduce with operator
- partial: fill in arguments early
- lru_cache: remember answers
- Decorators, and a difference on this Pi
- Build compose yourself

### Materials

- Half breadboard
- LED x 2
- Raspberry Pi 4 Model B
- Resistor x 2

### Check for understanding

Each student finishes "functools" 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: "Build compose yourself".

Chapter 4. About 50 min

## Project

A knob, an ADC and an LED, joined by a pipeline of generators.

Lesson | Time | What students do

Project: a streaming sensor pipeline A knob read through an MCP3008 becomes an endless stream of readings, then flows through generator stages (volts with map and partial, a moving average with a deque, a change filter) into an LED, assembled with reduce. | 50 min

- Run it: a sensor as a generator
- Stage 1: volts, with map and partial
- Stage 2: a moving average
- Stage 3: only the changes
- The sink: an LED
- Assemble it with reduce
- Make it yours

### Materials

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

### Check for understanding

Each student finishes "Project: a streaming sensor pipeline" 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: "Make it yours".

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.
