๐ Story: Zoggโs Supplies Are Confusing!
Zogg has learned to store supplies in a list. But now, there’s a new problemโฆ
๐คฏ “Wait! How do I know how many Oxygen Tanks I have? My list just has names!”
๐ธ “Zogg, use a dictionary!” the spaceshipโs AI suggests.
๐ก “A dictionary? Like a book?”
Not quite, Zogg! In Python, a dictionary helps us store pairs of informationโlike what we have and how many! ๐
๐ Step 1: Creating a Dictionary
A dictionary uses {}
curly brackets and stores key-value pairs:
supplies = {
"Oxygen Tanks": 3,
"Food Packs": 10,
"Fuel Cells": 5,
"Space Suits": 2
}
print(supplies)
๐ Keys = Names of supplies
๐ Values = How many we have
๐ Now, Zogg knows both what he has and how much!
๐ Step 2: Accessing Dictionary Values
Zogg wants to check how many Fuel Cells he has. Instead of searching a list, he can look it up instantly!
print(supplies["Fuel Cells"]) # Output: 5
โ Dictionaries let us quickly find what we need!
โ๏ธ Step 3: Updating Values in a Dictionary
Zogg just used one Oxygen Tank. Letโs update the count!
supplies["Oxygen Tanks"] -= 1 # Uses 1 Oxygen Tank
print(supplies)
๐ Dictionaries let us modify values easily!
โ Step 4: Adding & Removing Items
Zogg found a Repair Kit. Letโs add it to the dictionary!
supplies["Repair Kit"] = 1
print(supplies)
๐ Now, Repair Kit is part of the inventory!
But waitโone Space Suit is torn! Letโs remove it:
del supplies["Space Suits"]
print(supplies)
๐ Now the Space Suits are gone!
๐ Step 5: Looping Through a Dictionary
Zogg wants a full report of his supplies! Letโs loop through the dictionary:
for item, count in supplies.items():
print(item + ": " + str(count))
๐ข Now, Python lists all supplies and their amounts!
๐ฏ ๐ Challenge: Track Space Mission Supplies!
Zoggโs spaceship just restocked with:
โ
7 Water Bottles
โ
3 Energy Bars
๐ Mission: Add them to the dictionary and print the full list!
supplies = {
"Oxygen Tanks": 3,
"Food Packs": 10,
"Fuel Cells": 5
}
# Step 1: Add Water Bottles & Energy Bars
___
___
# Step 2: Print the updated inventory
print(___)
Can you complete the mission? Post your answer in the comments! ๐
โ ๏ธ Common Mistakes & Fixes!
โ Forgetting to use quotes for dictionary keys
supplies = {Oxygen Tanks: 3} # โ ERROR!
โ Fix:
supplies = {"Oxygen Tanks": 3} # โ
Works!
โ Using =
instead of :
in dictionaries
supplies = {"Oxygen Tanks" = 3} # โ ERROR!
โ Fix:
supplies = {"Oxygen Tanks": 3} # โ
Works!
๐ Fun Fact: Dictionaries Are Super Fast!
Looking up values in a dictionary is much faster than searching a list, especially when dealing with huge amounts of data! Thatโs why Python uses them in AI, games, and databases. ๐
๐ Next Mission: โFunctions โ Giving Python Superpowers!โ ๐ฆธโโ๏ธโก
Now that Zogg has organized his supplies, he wants to make his spaceship more efficient. But typing the same code over and over is getting frustrating! ๐คฏ
“Isn’t there a way to make Python do tasks for me?” Zogg wonders.
Yes! In the next mission, weโll learn about functionsโspecial mini-programs inside Python that can save time, reduce errors, and make your code super powerful!
๐ก Get ready to write your own functions and make Python work for you! ๐ป๐
See you in the next adventure, Junior Python Coder! ๐๐พ