What you will learn
- 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
Reading
Do Now
1. In your console
Type in the following
a_list = ['a', 'b', 'c', 'd', 'e']
print(len(a_list))
In your Notebook
Answer the following
- What type do you think a_list is? (Hint: look at the name of the variable)
- What does
lendo? - Brainstorm how you would print the first element from
a_list?
2. In your Console
Type the following
a_list = ['a', 'b', 'c', 'd', 'e']
print(a_list[0])
print(a_list[1])
print(a_list[2])
print(a_list[3])
print(a_list[4])
print(a_list[5])
print(a_list[6])
Continue In your Notebook
Explain what happens in the program
Lab
1. In your notebook
For each example below, predict what will be printed. Next, run the program and confirm what was output.
Example 1
a = ['a', 'b', 'c', 'd', 'e']
print(a[0])
print(a[3])
Example 2
a = ['a', 'b', 'c', 'd', 'e']
print(a[len(a) - 3])
Example 3
a = ['a', 'b', 'c', 'd', 'e']
print(a[len(a) - 6])
Example 4
a = ['a', 'b', 'c', 'd', 'e']
a[3] = 'haha'
print(a)
2. Create this game again using lists and indexes. Updated rules below
- Declare 10 prizes (prize0, prize1, prize2 at the top of your file), but store them all in a list.
- User picks a number.
- Print prize associated with the door user picked.
3. Create a quiz
Create a food quiz using lists and indexes.
- List of 6 different foods
- Ask the user 8 vague questions to find out what their favorite food it out of the list
- Update the score and print their top 2 favorite foods
Hint: google how to find the biggest number in a list python
Bonus
Research nested lists and work through the following:
Bonus Example 1
a = ['a', 'b', 'c', ['d', 'e']]
print(len(a))
Bonus Example 2
a = ['a', 'b', 'c', ['d', 'e']]
b = a[3]
print(b)
Bonus - In your Notebook
How would you access ‘d’ from the list a?
Check your understanding
Pick an answer for each question. You can try again until you get it right.
1. If pets = ['cat', 'dog', 'fish'], what is pets[1]?
'cat''fish''dog'- An error
List indexes start at 0, so index 1 is the second item.
2. What happens when you run print(pets[3]) with pets = ['cat', 'dog', 'fish']?
- An error, because index 3 is out of range
- It prints
'fish' - It prints nothing
- It prints
3
The list has three items with indexes 0 to 2, so index 3 does not exist.
3. What does len(pets) return for pets = ['cat', 'dog', 'fish']?
- 2
- 3
- 4
'fish'
The len function returns how many items are in the list, and the last valid index is one less than that.