Lists: One Box, Many Things!
How can one box remember a whole shopping list?
Real Python code
This is the code from the video. Read it out loud like a recipe, one line at a time!
groceries = ["apple", "banana", "cookie"]
print(groceries[0])
groceries.append("donut")
print(groceries[3])
print(len(groceries))
Quiz time!
In Python, what does print(fruits[1]) print?
fruits = ["apple", "berry", "melon"]
print(fruits[1])
Where lists hide in real apps
Game inventory
Swords, potions and coins in your hero's backpack
High score tables
The top ten players ranked from first to tenth
Music playlists
Songs lined up in order to play one after another
Chat messages
Every message sent in a group chat, in order
Variable or list?
Variable
- One box
- Remembers one value
- Good for your score or lives
- New value replaces the old
List
- Super-box with slots
- Remembers many items
- Each item has an index
- Add, remove, and count
One box holds one thing; a list holds them all!
Try it yourself!
In Scratch, make a list called backpack and add three quest items with blocks.
Bit is working on the next one: And / Or / Not: double choices