🐍
PYTHON QUESTAgent Academy
0 XP
150 XP to Next Rank
Mission #7

🎒Lesson 7: Lists -- The Super Backpack

Store and organize your inventory of items!

Lesson 7: Lists -- The Super Backpack

A single variable can only store one item at a time. In Python, a List is like a superhero backpack that stores as many items as you want inside square brackets [].

Key Concepts to Remember:

  • •
    Square Brackets [ ]: Lists are created with square brackets, separated by commas: inventory = ["Sword", "Shield"].
  • •
    0-Indexing: Python numbers slots starting at 0! inventory[0] gives "Sword", and inventory[1] gives "Shield".
  • •
    .append(): Adds a new item to the very end of your backpack list.
  • •
    .remove() & len(): Remove items with .remove() or .pop(), and count items with len(inventory).

💻Managing an Inventory List (IDE Syntax Colors)

backpack = ["Blaster", "Potion"]
backpack.append("Keycard")
 
print(backpack)
print(f"Total items: {len(backpack)}")
print(f"First item: {backpack[0]}")

💡 backpack stores 3 items, len() gives 3, and backpack[0] retrieves the first item.

💡 Pro Tip: Do not forget: the first item in any list is at position 0, not 1.