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.
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.
Beyond the for loop
Comprehensions, and what a for loop really does underneath.
| Lesson | Time | What students do |
|---|---|---|
| ComprehensionsList 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 |
|
| Iteratorsiter() 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 |
|
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".
Lazy streams
Generators that pause, take values in, hand over and clean up.
| Lesson | Time | What students do |
|---|---|---|
| Generators in depthWhat 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 |
|
| map and filterFunctions 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 |
|
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".
The toolkits
itertools and functools, and what to write when one is missing.
| Lesson | Time | What students do |
|---|---|---|
| itertoolsThe 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 |
|
| functoolsreduce 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 |
|
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".
Project
A knob, an ADC and an LED, joined by a pipeline of generators.
| Lesson | Time | What students do |
|---|---|---|
| Project: a streaming sensor pipelineA 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 |
|
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".
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.