Рубрики
Без рубрики

Новичок Python Tips Day – 01 Перечислять

# День 01 – Перечисрьер # предоставляет счетчик, который соответствует # Количество элементов в списке my_list = [‘h… Теги с Python, начинающими, CodeNewie, советы.

# Day 01 - Enumerate
# Provides counter that matches
# the number of items in the list

my_list = ['hello', 'world', '!']

# Without enumerate
for index in range(len(my_list)):
    print(index, my_list[index])
'''
Prints
0 hello
1 world
2 !
'''

# With enumerate
for counter, value in enumerate(my_list):
    print(counter, value)
'''
Prints
0 hello
1 world
2 !
'''

# Don't confuse it with index
# because, the counter can start from any integer
for counter, value in enumerate(my_list, 2):
    print(counter, value)
'''
Prints
2 hello
3 world
4 !
'''

Оригинал: “https://dev.to/paurakhsharma/day-01-enumerate-1558”