A. What Is a List?
๐ฏ Learning Goals
By the end of this lesson, you'll be able to:
- โ Understand what a list is
- โ Understand why we use lists
- โ Understand the difference between a list and a variable
Core Concepts
What is a list?
Lists is a way to store multiple items in a single variable.
๐๏ธ Analogy: a chest of drawers
Just like Chest of drawersLike putting many things into a chest of drawers!
- Chest of drawers = list
- Drawer slot = index (0, 1, 2, ...)
- Item = stored item
Variable vs. list
# Variable: stores only one value
fruit1 = "Apple"
fruit2 = "Banana"
fruit3 = "Orange"
# List: stores many values in one place
fruits = ["Apple", "Banana", "Orange"]
Advantages of a list
| Advantages | Description |
|---|---|
| Easy to manage | Manage many objects as one |
| Using loops | Easily process every item |
| Easy to add/remove | Manage items dynamically |
Using it in games
Example Code
# Fruit list
fruits = ["Apple", "Banana", "Orange"]
# Print the list
print(fruits) # ['Apple', 'Banana', 'Orange']
# Check the count
print(len(fruits)) # 3
What Happens
Three fruits are stored in the list!
Quiz
Quiz: Why use a list
Why do you use a list to manage multiple bullets in a game?
Show Answer
Answer: B) To manage multiple items with a single variable
Using a list, you can easily manage multiple bullets, enemies, items, and more with a single variable!
Basic list practice:
fruits = ["์ฌ๊ณผ", "๋ฐ๋๋", "์ค๋ ์ง"] Given a list:
- Symbol for making an empty list:
my_list = - Multiple items are separated by it (e.g.,
["์ฌ๊ณผ", "๋ฐ๋๋"]) - To check the number of items in the list (3):
(fruits)โ 3
๐ง Debug Clinic
Problem: managing multiple bullets is hard
โ Inefficient code
Problem: Creating separate variables makes it hard to manageโ Efficient code
Fix: Manage everything at once with a list!Try Coding It Yourself
Take what you learned with the blocks and write it yourself in Python code!
Printing a list
Create a list and print it.
Example Answer
Result: The list and its item count (3) are printed!
๐งฉ Key Takeaways
What you learned in this lesson:
| Concept | Description |
|---|---|
| Lists | Store multiple items in a single variable |
| Square brackets | [] to create a list |
| len() | Check the number of items in a list |
| Advantages | Easy management, loop usage, dynamic management |
Review Checklist
- [ ] Can you explain what a list is?
- [ ] Do you know the difference between a variable and a list?
- [ ] Do you understand why lists are used in games?