Packing Bundles

When you move, you do not carry every item one by one. You pack them into boxes. Computers can do the same: you can group multiple pieces of data together.

In Python, there are three main kinds of “bundles.” Their behavior depends on the brackets they use, so pay attention!


1. The most flexible bundle: list []

Lists use square brackets, and they are the most common bundle. You can add, remove, and change items freely.

Working with lists

shopping_list = ["apple", "banana"]

# 1. Add (append to the end)
shopping_list.append("grape")

# 2. Update (change by index)
# Computers count from 0, so index 0 is "apple".
shopping_list[0] = "strawberry"

# 3. Remove (delete)
del shopping_list[1]  # This removes "banana".

print(shopping_list)

Translation:

Remember shopping_list as ["apple", "banana"]

Add "grape" to the end of shopping_list
Change index 0 of shopping_list to "strawberry"
Delete index 1 of shopping_list

Print the result!

2. The stubborn, unchanging one: tuple ()

Tuples use parentheses. They look like lists, but once you create them, you cannot add or change items. (Think of a box sealed with tape.)

colors = ("red", "green", "blue")
# colors.append("yellow") -> error!
# colors[0] = "black" -> error!

3. A bundle with name tags: dictionary {}

Dictionaries use curly braces. Like a real dictionary, each item is a pair of key (name tag) and value (content).

Working with dictionaries

my_info = {
    "name": "Alex",
    "age": 20
}

# 1. Add & update (by name tag)
my_info["hobby"] = "Python"  # New tag (add)
my_info["age"] = 21          # Existing tag (update)

# 2. Remove (by name tag)
del my_info["name"]

print(my_info)

Translation:

Remember a tagged bundle called my_info:
    Put "Alex" under the "name" tag, and 20 under the "age" tag

Put "Python" under the "hobby" tag (add)
Change the "age" tag to 21 (update)
Delete the "name" tag

Print the result!

Next, we will learn how to use these bundles efficiently with loops.