Behind The Scene (For Loop in Python)
The for statement
The for statement in Python is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object. (https://docs.python.org/)
1
2
3
4
programming_language = ['C', 'C#', 'Java', 'Python']
for language in programming_language:
print(language)
Output
C
C#
Java
Python
We’ve constructed a list named programming_language in the example above. The value of language is at first set to first element list i.e “C” and then the print statement is executed. After that, language is updated with next element “C#” and print statement is executed again. This way the loop runs until and unless the last element of list is accessed.
Behind The Scene
From example shown above, we can now use for statement to iterate over each elements in sequence one by one. But do we know how it works? What’s underneath? How it is able to access the element one by one?
Let me explain it. Behind the scenes, the for statement calls iter() on the container object (programming_language) which returns an iter object. (<list_iterator object at 0x7f6099e53460>)
1
2
3
4
programming_language = ['C', 'C#', 'Java', 'Python']
z = iter(programming_language)
print(z)
# output - > <list_iterator object at 0x7f6099e53460>
The iter object has “__next__()” method which access elements in the container one at a time.
1
2
3
4
5
6
7
8
9
programming_language = ['C', 'C#', 'Java', 'Python']
z = iter(programming_language)
print(z)
# output - > <list_iterator object at 0x7f6099e53460>
print(z.__next__()) # output -> C
print(z.__next__()) # output -> C#
print(z.__next__()) # output -> Java
print(z.__next__()) # output -> Python
When there are no more elements, “__next__()” raises a StopIteration exception which tells the for loop to terminate.
1
2
3
4
5
6
7
8
9
10
11
programming_language = ['C', 'C#', 'Java', 'Python']
z = iter(programming_language)
print(z)
# output - > <list_iterator object at 0x7f6099e53460>
print(z.__next__()) # output -> C
print(z.__next__()) # output -> C#
print(z.__next__()) # output -> Java
print(z.__next__()) # output -> Python
print(z.__next__()) # output -> StopIteration Exception
We can also call the “__next__()” method using the built-in function next().
1
2
3
4
5
6
7
8
9
10
11
programming_language = ['C', 'C#', 'Java', 'Python']
z = iter(programming_language)
print(z)
# output - > <list_iterator object at 0x7f6099e53460>
print(next(z)) # output -> C
print(next(z)) # output -> C#
print(next(z)) # output -> Java
print(next(z)) # output -> Python
print(next(z)) # output -> StopIteration Exception
Leave a comment