PYTHON CORE · LESSON 30

Update the dictionary.

Change the key. · about 12 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Value can be replaced

In a dictionary, you can change the key. Put the key in square brackets.

player = {"score": 1}
player["score"] = 2
print(player["score"])

✦ Now two points.

STEP 2

Count through the old meaning

On the right, you can read the old points and add new ones, then save the result.

player = {"score": 1}
player["score"] = player["score"] + 3
print(player["score"])

✦ On screen 4.

IN-CLASS PRACTICE

Type the code yourself

Dial the program. In the middle line, the key appears twice.

player = {"score": 2}
player["score"] = player["score"] + 1
print(player["score"])
Open the editor for class work
HOMEWORK

Solve it yourself

Create a game dictionary with a «points» key and a value of 3. Add 2 to the points, save the new meaning in the same dictionary and output it.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework