Students will be able to...
- Define and identify: range.
- Use the
range
andlen
function to to update lists via for loops.
- 4.02 Slide Deck
- Do Now
- Lab - Getting Loopy (docx) (pdf)
- Associated Reading
- Read through the Do Now, lesson, and lab so that you are familiar with the requirements and can assist students.
Duration | Description |
---|---|
5 Minutes | Do Now |
10 Minutes | Lesson |
35 Minutes | Lab |
5 Minutes | Debrief |
- Display the Do Now on the board.
- Students experiment with and are introduced to the
range
function.
- Ask students what the
range
function did. - Remind them that there are reference docs online.
- Show the docs for the range function. Note that it can take in a third value that is optional.
- Work together with the students to write a
for
loop using therange
function.
- Ask the students what happened
- Ask the students why these values might be helpful
- They are a list of the indices!
- Ask students to write the first line of the loop on the board
- Work together as a class to come to a solution that is demonstrated for all to see.
In the sample "Function Contains Two Errors" code, we expect the return value True
, but we get an IndexError instead.
Suggest printing the values of the indices immediately before the line where the error appears (right after while statement).
First error: the index of the last character is 3
, so the initial value for j
should be different:
j = len(word2)-1
.
Second error: we need to include the character at index 0, so we need to change the while
condition:
while j >= 0:
- Students re-write the fruit pluralize program from yesterday's do now, but without creating a new list.
- Students write a function that reverses the letters in a string.
- Talk about any issues the students had with the lab today.
- Discuss how lists are mutable, so you don't have to return a new value. Instead, the list is just updated as the loop runs.
- If students are having a hard time with the fruit pluralize, consider altering the fruit program to not allow fruit that ends in y.
- Some students may have issues with grabbing the last item of a string, consider providing tips or scaffolding for students that are struggling with this.
- Go over the bonus question if any students got to it. Discuss having a function inside of the loop and how that operated.