Students will be able to...
- Define and identify: list, item, index, integer.
- Be able to access items from a list using the index.
- Create lists of different types.
- Use the length function.
- 2.04 Slide Deck
- Do Now
- Lab - Food Chooser (docx) (pdf).
- Solution (access protected resources by clicking on "Additional Curriculum Materials" on the TEALS Dashboard).
- Associated Readings 2.4
- Read through the do now, lesson, and lab so that you are familiar with the requirements and can assist students.
- Video Resources:
Duration | Description |
---|---|
5 Minutes | Do Now |
10 Minutes | Lesson |
35 Minutes | Lab |
5 Minutes | Debrief |
- Students follow instructions to create lists and use the
len
function.
- A list is a sequence of values. In a Python list, values can be any type. The values in a list are called elements or items.
- In Python, to create a list you must enclose items in square brackets, and separate multiple items with commas.
- Emphasize that you can have lists of any type (
int, float, string,
etc.) You can even have lists within lists (more on that later...)
- Ask students what
len
did when they used it in the Do Now. - Ask students how they tried to print the first item from a list. Was this what they were expecting?
- index: a map from the position in the list to the element stored there.
- 0-index: lists are 0 indexed. So the first element in the list is at an index of 0.
- Out-of-bounds: what happened when you tried to print an element of a list, using an index value that was too large?
- Ask students how they would access the last item in a list of unknown length. (Use the length function!)
- Ask a student to write on the board how they retrieved the last element of a list. Ask another student to write how they would get the second to last element of the list and so on.
- After accessing any list element you can change it. Take a moment to demonstrate this syntax before starting the lab.
Have students work through the following examples in their console.
Example 1
a_list = ['a', 'b', 'c', ['d', 'e']]
print(len(a_list))
Example 2
a_list = ['a', 'b', 'c', ['d', 'e']]
b_list = a_list[3]
print(b_list)
In their notebooks, have students explain how they would access the element d
, starting from the definition of a_list
.
- Practice accessing and updating items in a list.
- Implement program from last lab using lists.
- Create a quiz program.
- Check student progress and completion of the lab. Wrap up by taking any final questions.
If students are moving quickly, you can continue the topic of nested lists. Start off with a simple nested list like ['a', 'b', 'c', [1, 2, 3]]
. Ask the students to guess the length. Ask the students to guess how they would access the item '1' from that list!