Skip to content

Python iteration

  • by

The Repeated execution of a set of statements is called iteration in Python. Iteration means taking an object like a list or a tuple (an object that has more than one value) and going through those values.

Python iteration

Simple example code Using Iterations in Python Effectively.

Use of for-in (or for each)

The iterator fetches each component and prints data while looping.

cars = ["BMW", "Audi", "McLaren"]
for x in cars:
    print(x)

Output:

Python iteration

Enumerate:

The Enumerate is a built-in python function that takes input as an iterator, list, etc, and returns a tuple containing an index and data at that index in the iterator sequence.

cars = ["BMW", "Audi", "McLaren"]
for x in enumerate(cars):
    print (x[0], x[1])

Output:

0 BMW
1 Audi
2 McLaren

Do comment if you have any doubts or suggestions on this Python basic topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *