Python: Lists
Store multiple items in a single variable using Index positioning.
The Hook
Imagine you are going shopping. You buy apples, bananas, and milk.
Do you want 3 separate pockets (variables) to carry them? Or ONE bag (List) to hold them all?
A List allows us to store many items in a single variable.
Worked Example
In Python, we count starting from ZERO.
Updating Items
You can replace any item by targeting its position.
Predict Output
Targeting the middle item. What exactly prints?
print(nums[1])
Food List
"Create a list of 3 foods. Update Index 1 to be 'Pizza' and print the whole list."
Out of Range!
"There are only 2 items in this list (Index 0 and 1). Fix the error to print the number 50."
Team Picker
Independent Construction Required
- Create a list of 4 names (e.g.
"A", "B", "C", "D"). - Ask the user for a number between
0 and 3. - Convert to
int(). - Print the name at that index from the list.