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.

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.

LessonTimeWhat 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
  1. Run it: the loop you already know
  2. The same list in one line
  3. Choose a value per item
  4. Dicts and sets too
  5. Nested comprehensions
  6. Generator expressions and an alarm
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
  1. Run it: next, next, next
  2. A for loop, by hand
  3. One pass only
  4. Write your own iterator
  5. Iterables start over
  6. 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.

LessonTimeWhat 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
  1. Run it: a function that pauses
  2. Forever is fine
  3. send(): values go in, too
  4. yield from, and a return value
  5. close() and finally
  6. A lazy pipeline
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
  1. Run it: map
  2. filter and lambda
  3. Two lists at once
  4. Functions in a dict
  5. Key functions
  6. 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.

LessonTimeWhat 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
  1. Run it: count and islice
  2. cycle: a watch rota
  3. Running totals and differences
  4. groupby needs sorted data
  5. Combinations and products
  6. chain, takewhile, dropwhile
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
  1. Run it: reduce
  2. reduce with operator
  3. partial: fill in arguments early
  4. lru_cache: remember answers
  5. Decorators, and a difference on this Pi
  6. 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.

LessonTimeWhat 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
  1. Run it: a sensor as a generator
  2. Stage 1: volts, with map and partial
  3. Stage 2: a moving average
  4. Stage 3: only the changes
  5. The sink: an LED
  6. Assemble it with reduce
  7. 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.