How does the for loop work in Python?
Most container objects in Python can be looped over using a for statement. Behind the scenes, the for statement calls the built-in iter() function on the container object which returns an iterator. The iterator object has the __next__() method which is used to access elements in the container one at a time. When there are no more elements left, __next__() raises the StopIteration exception which tells the for loop that we’re done....