Lesson plan

Python from zero lesson plan

Beginner Python on a Raspberry Pi: print, variables, decisions, loops, lists, functions, dicts and strings, then a button with gpiozero and a working pedestrian crossing.

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

  • Make the Pi talk, remember and decide.
  • Loops, lists, functions and dicts: the tools every program is made of.
  • Read a button properly, with gpiozero.
  • A pedestrian crossing built from everything in the course.

Materials for the whole unit

  • Half breadboard
  • LED x 2
  • Motion sensor
  • Pushbutton
  • Raspberry Pi 4 Model B
  • Resistor x 2
  • Traffic light module

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

First words

Make the Pi talk, remember and decide.

LessonTimeWhat students do
Print and variablesYour first Python on a Raspberry Pi: print() to talk, variables to remember, a little math, f-strings, the three types you meet first, and an LED switched on from code.45 min
  1. Run your first program
  2. A variable is a name for a value
  3. Do math with a variable
  4. f-strings: values inside text
  5. Three types of value
  6. Switch on a real LED
Decisionsif, elif and else: comparisons, True and False, and, or and not, why indentation is part of the code, and a motion alarm that decides twice a second whether to light up.45 min
  1. Run it: one of two paths
  2. Send it the other way
  3. elif: more than two paths
  4. True, False, and, or, not
  5. Decide on a real sensor
  6. Indentation is the meaning

Materials

  • Half breadboard
  • LED x 2
  • Motion sensor
  • Raspberry Pi 4 Model B
  • Resistor x 2

Check for understanding

Each student finishes "Decisions" 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: "Indentation is the meaning".

Chapter 2. About 2 h 15 min

Repeat and organize

Loops, lists, functions and dicts: the tools every program is made of.

LessonTimeWhat students do
Loops and listsfor and range, lists you can index and grow, a list of real LEDs lit one after another, while loops that run forever, and break to stop one when the job is done.45 min
  1. Run it: three laps
  2. Count from one
  3. A list holds many values
  4. A list of real LEDs
  5. while: keep going
  6. Stop with break
Functionsdef to name a few lines and run them by name, parameters and defaults, return to hand a value back, functions that choose a light, and why a value goes in and comes out instead of being shared.45 min
  1. Run it: one name, three flashes
  2. A parameter with a default
  3. return hands a value back
  4. A function that picks a light
  5. Values go in and come out
  6. Why functions
Dicts and stringsDictionaries that look a value up by its name, strings you can split, clean and change, and a little command line on the Pi: type "red on" and the red light obeys.45 min
  1. Run it: look up by name
  2. Take a string apart
  3. A dict of lights
  4. Type commands into the Pi
  5. A name that is not there
  6. A status command, in order

Materials

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Check for understanding

Each student finishes "Dicts and strings" 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: "A status command, in order".

Chapter 3. About 45 min

Touch the world

Read a button properly, with gpiozero.

LessonTimeWhat students do
Reading a button with gpiozeroA pushbutton on GPIO21: asking with is_pressed, why a pull-up keeps the pin from floating, waiting for a press, contact bounce, header pin numbers, timeouts, and callbacks that run when it happens.45 min
  1. Run it and hold the button
  2. Why there is a pull-up
  3. Wait for a press
  4. Bounce, and header pin numbers
  5. Wait, but not forever
  6. Callbacks: when it happens

Materials

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Check for understanding

Each student finishes "Reading a button with gpiozero" 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: "Callbacks: when it happens".

Chapter 4. About 45 min

Boss project

A pedestrian crossing built from everything in the course.

LessonTimeWhat students do
Project: traffic lightsThe boss project: a pedestrian crossing on the traffic light module and a button, built from everything in the course: a phase function, the sequence as a list, a loop waiting on the button, a counter and a flashing amber.45 min
  1. Run it: green for the cars
  2. One function forevery phase
  3. The sequence is data
  4. Count the crossings
  5. Flash the amber
  6. What you built

Materials

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Check for understanding

Each student finishes "Project: traffic lights" 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 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.