PYTHON CORE · LESSON 32

Number and element

Function enumerate · about 12 minutes

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

Get the number with the thing.

enumerate gives the loop two variables: the place number and the element itself.

colors = ["синий", "зелёный"]
for index, color in enumerate(colors):
    print(index, color)

✦ The lines 0 синий and 1 зелёный will appear.

STEP 2

Numbers start from scratch.

The index number grows at each step of the loop, and the color is changed to the next color.

names = ["Аня", "Лев"]
for index, name in enumerate(names):
    print(index, name)

✦ First name gets number 0.

IN-CLASS PRACTICE

Type the code yourself

Dial the loop with the number and element manually.

colors = ["синий", "зелёный"]
for index, color in enumerate(colors):
    print(index, color)
Open the editor for class work
HOMEWORK

Solve it yourself

Create pets from «кот» and «пёс». With enumerate, get index and pet; output both via print.

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

Opening up your homework