๐ Story: A Supply Crisis on Zoggโs Ship!
Zogg is ready to explore new planets, but suddenlyโฆ
๐ธ “Warning! Low supplies detected!”
Zogg checks his shipโs storage. “Oh no! I need to organize my supplies. Writing each one separately will take forever!”
๐ค “Zogg, use a Python list! It will help you store and manage your supplies easily.”
Zogg scratches his head. “A list? Tell me more!”
๐ฆ What is a List?
A list in Python is like a backpack where you can store multiple things together! Instead of using separate variables, you can keep everything in one place.
For example, instead of:
supply1 = "Oxygen Tanks"
supply2 = "Food Packs"
supply3 = "Fuel Cells"
We can store them in a list:
supplies = ["Oxygen Tanks", "Food Packs", "Fuel Cells"]
Now, all our items are organized in one variable! ๐
๐ Step 1: Creating a List
Let’s help Zogg store his supplies in a list!
supplies = ["Oxygen Tanks", "Food Packs", "Fuel Cells", "Space Suits", "Medical Kit"]
print(supplies)
โ Run this code โ it prints everything in the list!
๐ Step 2: Accessing Items in a List
Lists have positions (called indexes) to help find items. Python starts counting from 0!
Index | Item |
---|---|
0 | Oxygen Tanks |
1 | Food Packs |
2 | Fuel Cells |
3 | Space Suits |
4 | Medical Kit |
๐ To get the first item:
print(supplies[0]) # Oxygen Tanks
๐ To get the third item:
print(supplies[2]) # Fuel Cells
โจ Step 3: Adding and Removing Items
Zogg finds a new tool for space repairs! Letโs add it to the list.
supplies.append("Repair Kit")
print(supplies)
๐ .append()
adds a new item to the end of the list!
Now, letโs say Zogg uses a Medical Kit and needs to remove it:
supplies.remove("Medical Kit")
print(supplies)
๐ก .remove()
takes an item out of the list!
๐ฏ ๐ Challenge: Organizing Zoggโs Inventory!
Zogg wants to:
1๏ธโฃ Add “Solar Batteries” to the list.
2๏ธโฃ Remove “Food Packs” after eating them.
3๏ธโฃ Print the updated list.
๐ Complete the code below!
supplies = ["Oxygen Tanks", "Food Packs", "Fuel Cells", "Space Suits", "Medical Kit"]
# Step 1: Add Solar Batteries
supplies.append(______)
# Step 2: Remove Food Packs
supplies.remove(______)
# Step 3: Print updated list
print(______)
๐ Can you complete the code? Try it and post your answer below!
โ ๏ธ Common Mistakes and Fixes!
โ Forgetting list indexes start at 0
print(supplies[5]) # โ ERROR! No index 5 (list has only 5 items)
โ Fix: Use a valid index!
print(supplies[2]) # โ
Works fine!
๐ Fun Fact: Lists Are Everywhere!
Lists are used in video games, music playlists, and even shopping carts in online stores! ๐ฎ๐
Minecraft uses lists to store player inventory, and YouTube uses lists for your favorite videos! ๐ถ
๐ Whatโs Next?
Zogg has organized his inventory with lists! But what if he needs to check each item automatically?
๐ง Next Mission: “Loops โ Checking Zoggโs Supplies!” ๐
๐ Comment below!
1๏ธโฃ What supplies would you add to Zoggโs list?
2๏ธโฃ Did you complete the challenge? Share your code!
See you in the next adventure, Junior Python Coder! ๐๐ก