MemotivaIterators and iterables

How does the for loop work internally?

Iterators and iterables

Audio flashcard · 0:15

Nortren·

How does the for loop work internally?

0:15

A for loop calls iter on the iterable to get an iterator, then repeatedly calls next on the iterator. Each call returns the next value, which is bound to the loop variable. When the iterator is exhausted, it raises StopIteration, and the loop exits cleanly without propagating the exception.
docs.python.org