Sobes.tech
Junior — Middle

Can you give an example of implementing an iteration mechanism over a collection of elements?

sobes.tech AI

Answer from AI

In Python, iterating over a collection (like a list) is usually done with a for loop:

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(f'Fruit: {fruit}')

This code sequentially goes through each element in the list fruits and prints it. This method is convenient and readable.

You can also use the enumerate function if you need the index of the element:

for index, fruit in enumerate(fruits):
    print(f'{index}: {fruit}')