Study Guide Instructor view

Python Loops, Lists and Functions

Teaching notes for Lesson 5.19, with controls for presenting it.

Keep this page on your own screen. Share only the lesson window. The lesson window holds no teaching notes, so sharing it cannot show them.

Presenting

  1. Open the lesson in its own window and share that window, not this one.
  2. Use Next and Previous here, or the lesson’s own, to move through the lesson. Next reveals one thing at a time: the next idea, or the answer to the question on screen, then the next step. Previous takes the last reveal back. The line below says what is on screen and what Next will show.
  3. Reveal hints and answers when the class is ready, and reset a demonstration before using it again.

The controls need JavaScript and a browser that supports BroadcastChannel. Without them, open the lesson in presentation mode and use its own controls.

The two windows talk to each other only inside this browser, on this device. Nothing is sent to a server, and no account is needed. A second device (a phone or another tablet) cannot follow the lesson window yet.

Lesson plan

Level
Intermediate
Time
About 90 minutes; the steps below add up to 100

Learners carry loops, arrays and subroutines from pseudocode into Python, build a small quiz marker one function at a time, and meet the usual faults: range stopping before its last number, a list index that is too big, an infinite while, and a function that prints instead of returning. All questions can be answered on paper.

Objectives

  • Predict what a for loop with range produces, including where it starts and stops
  • Trace a while loop and explain why its condition must be able to change
  • Create a list, read an item by its index, find its length and add to it with append
  • Loop over a list to total its items
  • Write and use a function with parameters that returns a value, and explain how it differs from printing
  • Combine a loop, a list and functions in a small modular program, and find faults in one

Before the lesson

  • Mini whiteboards for predicting output and trace tables.
  • Optional: a computer with a free Python environment for checking predictions.
  • Optional: the quiz program printed so that pupils can annotate the three steps.

Steps

  1. Introduction Opening

    On screen: the lesson’s title, module and number; Next adds its objectives and key terms, as the lesson page lists them; Next again begins the first step.

    Objectives on screen

    • Predict what a for loop with range produces, including where it starts and stops
    • Trace a while loop and explain why its condition must be able to change
    • Create a list, read an item by its index, find its length and add to it with append
    • Loop over a list to total its items
    • Write and use a function with parameters that returns a value, and explain how it differs from printing
    • Combine a loop, a list and functions in a small modular program, and find faults in one

    Purpose: Learners carry loops, arrays and subroutines from pseudocode into Python, build a small quiz marker one function at a time, and meet the usual faults: range stopping before its last number, a list index that is too big, an infinite while, and a function that prints instead of returning. All questions can be answered on paper.

  2. 1. Fifty lines of the same thing Think 5 min

    Purpose: Motivate loops and functions from the cost of copying lines.

    Ask

    • What is wrong with 50 print lines?
    • What would you want to say instead?

    Responses to expect

    • Slow, error-prone and hard to change
    • Repeat this 50 times with a counter

    Misconceptions to challenge

    • A learner may believe: “Copying lines is fine if the work is quick.” Respond: Ask what changes when the quiz has 80 questions.

    Checking understanding: A show of hands for who would rather write one instruction than fifty.

    In the lesson window

    • Think it through: You could write 50 print lines, one for each heading. Why is that a poor way to write the program, and what would you want the language to let you say instead?
  3. 2. Repeating with for and range Predict 12 min

    Purpose: Predict range(5), then state the stop-before rule and practise forms of range.

    Ask

    • What does range(5) produce?
    • Which loop gives 1 to 50?

    Responses to expect

    • 0 to 4
    • range(1, 51)

    Misconceptions to challenge

    • A learner may believe: “range(5) gives 1 to 5.” Respond: It starts at 0 and stops before 5.
    • A learner may believe: “range(1, 5) includes 5.” Respond: The stop number is never included.

    On the board or with the pen

    • Write the numbers under each range call and cross out the stop number.

    Checking understanding: The range table and the range-stop check: pupils who choose range(50) or range(1, 50) mix up the start or the stop.

    Depending on the answers

    • If pupils choose range(1, 50): ask them to write the last number the loop gives
    • If pupils are secure: ask for range(10, 0, -3) before they see the table

    In the lesson window

    • Think it through: Predict what this program shows. How many lines does it show, what is the first number and what is the last? Are you surprised by either?
    • Check your understanding: Which loop shows the headings Question 1 to Question 50, using print(“Question”, number)?
  4. 3. Repeating until something changes: while Predict 10 min

    Purpose: Trace a while loop and see why the condition must be able to change.

    Ask

    • What does the program show on its last line?
    • What happens without the line that adds 15?

    Responses to expect

    • 2 40
    • It never stops

    Misconceptions to challenge

    • A learner may believe: “The loop runs while savings is 40.” Respond: The condition is savings < 40, which is false at 40.
    • A learner may believe: “A loop without a change just ends.” Respond: The condition is the same each time, so it stays true.

    Checking understanding: The while trace, and the while-final check: pupils who choose “stops after one pass” have not read the condition as a test each time.

    Depending on the answers

    • If pupils believe the loop ends at 40 because it reached 40: ask for the condition at 40 and the word to use for true or false

    In the lesson window

    • Think it through: Predict what the program shows on its last line. How many times does the loop run, and what makes it stop?
    • Check your understanding: A programmer forgets the line savings = savings + 15 inside the loop. What happens when the program runs?
  5. 4. Keeping many values together: lists Discover 8 min

    Purpose: Predict a list program and meet index from 0, len and IndexError.

    Ask

    • What does pets[1] show?
    • What is the last index of a list of four items?

    Responses to expect

    • dog
    • 3

    Misconceptions to challenge

    • A learner may believe: “The first item has index 1.” Respond: In a Python list the first index is 0, as one of the choices for pseudocode arrays in Lesson 10.
    • A learner may believe: “The last index is len(list).” Respond: It is len(list) - 1.

    Checking understanding: The last-index check, with four options, each a different miscount.

    Depending on the answers

    • If pupils choose 4: ask them to write the indexes 0, 1, 2 and count them

    English as an additional language

    • Practise index as position and “out of range”.

    In the lesson window

    • Think it through: Predict the two lines this shows. What does the number in the square brackets, in pets[1], seem to mean?
    • Check your understanding: A list holds 4 items. What is the index of the last item?
  6. 5. Looping over a list Explain 9 min

    Purpose: Two ways to go through a list, and a total built with a loop.

    Ask

    • What does the total become after each score?
    • What is the last line of the second program?

    Responses to expect

    • 7, 16, 20
    • 3 bird

    Misconceptions to challenge

    • A learner may believe: “The loop variable is the index in for score in scores.” Respond: It is the item itself. Use range(len(list)) for the index.

    Checking understanding: The last-line check: the 4 bird option shows who has not remembered range stops early.

    Support

    • Give the trace table with the first two rows filled in.

    In the lesson window

    • Check your understanding: What is the last line shown by the second program, the one that prints i and pets[i]?
  7. 6. Giving a job a name Discover 10 min

    Purpose: Pupils say why a named block is needed before def, parameters and return are shown.

    Ask

    • Why not copy the marking rule five times?
    • What is the difference between print and return?

    Responses to expect

    • A mistake would have to be fixed in five places
    • print shows; return gives the value back to the program

    Misconceptions to challenge

    • A learner may believe: “A function that prints can be used in a sum.” Respond: It gives back None, which cannot be added.
    • A learner may believe: “Parameters are the same as the values passed in.” Respond: The parameter is the name inside the definition; the value is passed when it is called.

    On the board or with the pen

    • Draw a box labelled double with an arrow in labelled 4 and an arrow out labelled 8.

    Checking understanding: The return-vs-print check.

    Depending on the answers

    • If pupils choose the print option: ask what total + (the result) would add if the function only shows a number

    In the lesson window

    • Think it through: The marking rule takes four lines of Python. The quiz has five questions, so the lines would be copied five times. What could go wrong? What would you prefer to be able to do?
    • Check your understanding: A function is meant to give back the larger of two numbers so that a program can add it to a total. Which ending of the function is right?
  8. 7. Building the quiz marker: one function Explain 6 min

    Purpose: Step one of the build: the marking function on its own.

    Ask

    • What do mark("b", "b") and mark("a", "d") give?

    Responses to expect

    • 1 and 0

    Misconceptions to challenge

    • A learner may believe: “A function always returns the same value.” Respond: It returns 1 or 0, depending on whether the two values match.

    Checking understanding: Pupils write two more calls and their results.

    In the lesson window

    • Check your understanding: What do mark(“b”, “b”) and mark(“a”, “d”) give back?
  9. 8. Building the quiz marker: a loop over two lists Experiment 10 min

    Purpose: Step two: a loop over two lists that uses the function and builds a score.

    Ask

    • What are answers[i], key[i] and the score at each i?

    Responses to expect

    • b b 1 1; a d 0 1; a a 1 2; c c 1 3; d b 0 3

    Misconceptions to challenge

    • A learner may believe: “i starts at 1.” Respond: range(len(key)) starts at 0, which is the first item.

    Checking understanding: The trace table: pupils who write the wrong answers[i] have mixed up the two lists.

    Depending on the answers

    • If pupils start at index 1: point to the first item of each list and ask for its index
    • If pupils trace easily: ask for the score if the first answer were wrong

    Support

    • Provide the two lists as a table with indexes above them.

    In the lesson window

    • Think it through: Trace the loop on paper: for each value of i, write answers[i], key[i], what mark gives, and the score afterwards. What does the last line show?
  10. 9. Building the quiz marker: a second function Explain 7 min

    Purpose: Step three: a second function, then the whole program.

    Ask

    • What percentage is 3 out of 5?
    • Which grade, and why not Merit?

    Responses to expect

    • 60
    • Pass, because 60 is at least 50 but below 80

    Misconceptions to challenge

    • A learner may believe: “The pass mark is a number of questions.” Respond: The function compares a percentage.

    Checking understanding: The grade-calls check.

    To go further

    • Change the boundaries so that 4 out of 5 is a Distinction.

    In the lesson window

    • Check your understanding: Using the score of 3 out of 5, what does the last line of the program show, and why?
  11. 10. Spot the mistake Apply 8 min

    Purpose: Diagnose an off-by-one in range and a function that prints instead of returning.

    Ask

    • What happens when i reaches 5?
    • What does score + None do?

    Responses to expect

    • An IndexError: the indexes are 0 to 4
    • A TypeError

    Misconceptions to challenge

    • A learner may believe: “range(len(key) + 1) includes every question.” Respond: It goes one past the last index.

    Checking understanding: Pupils write the corrected line on a whiteboard.

    Depending on the answers

    • If pupils think the extra question is marked: ask what answers[5] is on a list of five items

    In the lesson window

    • Apply it: What happens when Sam’s program runs, and why?
    • Apply it: What happens when the program runs?
  12. 11. Write a function of your own Apply 10 min

    Purpose: Write a function with a loop, a condition, a count and a return.

    Ask

    • Where does the count start?
    • Where does the return go?

    Responses to expect

    • At 0, before the loop
    • After the loop, at the indent of for

    Misconceptions to challenge

    • A learner may believe: “return can go inside the loop.” Respond: It would end the function after the first item.

    Checking understanding: Check the example call: the answer is 3, since 6 is not greater than 6.

    Support

    • Give the first and last lines of the function.

    Stretch

    • Change it to count the scores that are at least the limit.

    In the lesson window

    • Think it through: Write a function count_high(scores, limit) that returns how many of the numbers in the list scores are greater than limit. Then say what count_high([7, 9, 4, 10, 6], 6) gives.
  13. 12. Show what you know Consolidate 5 min

    Purpose: Explanation of modular programming and a last context for return.

    Ask

    • Why is the quiz easier to change as separate functions?
    • Where should the return go in a total function?

    Responses to expect

    • One job each, tested alone, changed in one place
    • After the loop

    Misconceptions to challenge

    • A learner may believe: “return inside the loop returns each price in turn.” Respond: A function returns once, then ends.

    Checking understanding: Collect the “least sure” questions.

    Depending on the answers

    • If pupils choose print for the total: ask what the program would add it to

    In the lesson window

    • Think it through: Explain why the quiz program is easier to read, test and change when the marking rule and the grading rule are separate functions, compared with one long program. Use one example change.
    • Apply it: A fictional canteen program has prices = [2, 3, 5] and a function total(prices) that loops over the list adding each price to a running total. Which statement is correct about the function’s last line?
    • Think it through: Which part of this lesson are you least sure about? Write one question you would ask your teacher.
  14. Lesson recap Closing

    On screen: the lesson’s objectives again; Next adds the closing question, which asks the class to explain each one in their own words without looking back. It says nothing about what anyone has learnt: use the answers to decide what to revisit.

    Points to listen for

    • range stops before its last number and starts at 0 by default; for the numbers 1 to N use range(1, N + 1).
    • while repeats while its condition is true, and something inside must change it.
    • List indexes start at 0; the last is len(list) - 1.
    • A function receives values through parameters and returns a result; print only shows it.

Consolidation

  • range stops before its last number and starts at 0 by default; for the numbers 1 to N use range(1, N + 1).
  • while repeats while its condition is true, and something inside must change it.
  • List indexes start at 0; the last is len(list) - 1.
  • A function receives values through parameters and returns a result; print only shows it.