PYTHON CORE · LESSON 46

All dictionary pairs

Method of items · about 12 minutes

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

Two pieces.

items gives each pair of dictionaries a key and a value. The loop can take them into two variables.

scores = {"Лев": 2, "Аня": 3}
for name, score in scores.items():
    print(name, score)

✦ On each line will appear a name and points.

STEP 2

Intra-loop retreat

The print string belongs to the loop, so it needs four spaces before it.

ages = {"кот": 2}
for pet, age in ages.items():
    print(pet, age)

✦ «кот 2» will appear.

IN-CLASS PRACTICE

Type the code yourself

Type a dictionary pair loop. Check the indentation.

ages = {"кот": 2, "пёс": 4}
for pet, age in ages.items():
    print(pet, age)
Open the editor for class work
HOMEWORK

Solve it yourself

Create scores: Leo has 2 points, Ani 3. Loop items display the name and points of each pair.

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

Opening up your homework