Python - 13

Python - 13: Dictionaries

Dictionaries store related information using keys and values.

Your challenge: Read values from a dictionary, change one value, then add a new key.

✦ Python Editor
▶ Output

Keys and values

A dictionary uses curly braces. Each key points to a value: "name": "Alex"

In this example, "name" is the key and "Alex" is its value.

Access a value

Use the key inside square brackets: player["score"]

Change or add data

You can change an existing value: player["score"] = 250

You can also create a new key the same way: player["lives"] = 3

Try it: Add another key such as "status" or "team" and give it a value.

Good Python habit: Use clear dictionary keys that describe what each value represents.

👀 Show Example Solution
player = {
    "name": "Alex",
    "score": 100,
    "level": 5
}

print(player)

print(player["name"])
print(player["score"])

player["score"] = 250

player["lives"] = 3

print(player)

Dictionaries make it easy to group related information together and access each value by its key.