Welcome back to Day 3, Python dynamic duo! π If you survived Day 2, you now know how to create variables and throw strings, integers, floats, and booleans into their own little cardboard boxes. π¦
But what happens when youβre building a game and your character needs an inventory? Or you're making a shopping list app? Creating 50 different variables like item1, item2, item3 will make you want to throw your router out the window. πͺπ»
Today, we are leveling up our storage game. We are moving out of single cardboard boxes and packing a Virtual Backpack: Enter Lists! ππ
π What is a List?
In Python, a List is a data structure used to store a collection of items in one single variable. Think of it like a backpack where you can stuff multiple things inside, keep them in a specific order, and pull them out whenever you need them.
Creating a list is simple. You use square brackets [] and separate your items with commas:
# Packing our survival backpack πΊοΈ
backpack = ["map", "flashlight", "water bottle", "protein bar"]
print(backpack)
# Prints: ['map', 'flashlight', 'water bottle', 'protein bar']
The coolest part? Python lists donβt care what you put inside. You can mix strings, integers, and booleans all in one single backpack (though usually, it makes the most sense to keep similar things together).
π€― The First Rule of Coding Club: We Start Counting at Zero!
Here is where programming turns your brain upside down. π§ π
If I asked you what the first item in our backpack list is, youβd logically say "map". And you'd be right in human language. But in Python-speak, computer memory starts counting at 0.
This is called Indexing.
To pull a specific item out of your backpack, you write the name of the list followed by the item's position (index) inside square brackets:
backpack = ["map", "flashlight", "water bottle", "protein bar"]
# Pulling out the items using their index π
print(backpack[0]) # Prints: map (The absolute first item!)
print(backpack[1]) # Prints: flashlight (The second item!)
print(backpack[3]) # Prints: protein bar
β οΈ Tantrum Alert: If you try to print backpack[4], Python will immediately crash and scream:
IndexError: list index out of range.
python
Why? Because there is no 5th item! Always remember: if your list has 4 items, the indices go from 0 to 3.
π οΈ Modifying the Backpack (List Methods)
The best thing about a backpack is that it isnβt glued shut. You can add things to it, change things inside it, or chuck things away when you don't need them anymore.
Here are the three most common magic spells youβll use with lists:
1. Changing an item (Reassignment)
Did your flashlight break? Let's swap it out for a laser pointer:
backpack[1] = "laser pointer"
print(backpack)
# Prints: ['map', 'laser pointer', 'water bottle', 'protein bar']
2. Adding an item (.append())
Found some gold coins on the floor? Letβs stuff them into the bottom of the backpack using .append():
backpack.append("gold coins")
print(backpack)
# Prints: ['map', 'laser pointer', 'water bottle', 'protein bar', 'gold coins']
3. Removing an item (.remove())
Got hungry and ate the protein bar? We can remove it by name:
backpack.remove("protein bar")
print(backpack)
# Prints: ['map', 'laser pointer', 'water bottle', 'gold coins']
Thats a wrap for the day β±οΈ
π Today's Challenge π
Time to put your virtual backpack to the test!
Create a list called gaming_squad containing the names of 3 of your friends (or fictional characters).
Print out the second person in that list (Remember the zero-counting rule! π).
Use .append() to add a 4th teammate to the squad.
Print the final list to the console to make sure they made the cut.
Drop your code or your squad lineup in the comments below! Are you starting to see how powerful these boxes can get? Tomorrow, we are looking at Tuplesβwhich are basically lists that have been permanently superglued shut. π
See you on Day 4! ππ»π
Top comments (0)