June 2017
Beginner
352 pages
8h 39m
English
As usual, a demonstration at the Python REPL will help all of these concepts crystallize into something you can work with. We start with a list of the names of the seasons as our iterable object:
>>> iterable = ['Spring', 'Summer', 'Autumn', 'Winter']
We then ask our iterable object to give us an iterator using the iter() built-in:
>>> iterator = iter(iterable)
Next we request a value from the iterator object using the next() built-in:
>>> next(iterator)'Spring'
Each call to next() moves the iterator through the sequence:
>>> next(iterator)'Summer'>>> next(iterator)'Autumn'>>> next(iterator)'Winter'
But what happens when we reach the end?
>>> next(iterator)Traceback (most recent call last): File "<stdin>", ...
Read now
Unlock full access