How for loop in Python works internally?

Before proceeding to this section, you should have a prior understanding of Python Iterators.

Firstly, lets see how a simple for loops in Python looks like.

Example: This Python code iterates through a list called fruits, containing “apple”, “orange” and “kiwi.” It prints each fruit name on a separate line, displaying them in the order they appear in the list.

Python3
fruits = ["apple", "orange", "kiwi"]

for fruit in fruits:

    print(fruit)

Output
apple
orange
kiwi

Here we can see the for loops in Python that iterates over iterable object fruit which is a list. Lists, sets, dictionaries are few iterable objects while an integer object is not an iterable object. For loops can iterate over any of these iterable objects.

This Python code manually iterates through a list of fruits using an iterator. It prints each fruit’s name one by one and stops when there are no more items in the list.

Python3
fruits = ["apple", "orange", "kiwi"]
iter_obj = iter(fruits)
while True:
    try:
        fruit = next(iter_obj)
        print(fruit)
    except StopIteration:
        break

Output
apple
orange
kiwi

We can see that under the hood we are calling iter() and next() method. 

We have covered Python Loops in this article. We also saw how to use for loop, while loop and nested loop in Python. This article provides different use-case scenarios and examples to demonstrate working of loops and give clear understanding.

Learn More on Loops:



Loops in Python – For, While and Nested Loops

Python programming language provides two types of Python loopshecking time. In this article, we will look at Python loops and understand their working with the help of examp – For loop and While loop to handle looping requirements. Loops in Python provides three ways for executing the loops.

While all the ways provide similar basic functionality, they differ in their syntax and condition-checking time. In this article, we will look at Python loops and understand their working with the help of examples.

Similar Reads

While Loop in Python

In Python, a while loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed....

For Loop in Python

For loops are used for sequential traversal. For example: traversing a list or string or array etc. In Python, there is “for in” loop which is similar to foreach loop in other languages. Let us learn how to use for loops in Python for sequential traversals with examples....

Nested Loops in Python

Python programming language allows to use one loop inside another loop which is called nested loop. Following section shows few examples to illustrate the concept....

Loop Control Statements

Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements....

How for loop in Python works internally?

Before proceeding to this section, you should have a prior understanding of Python Iterators....