An iterator is an object that contains a countable number of values.
An iterator is an object that can be iterated upon, meaning that you can traverse through all the values.
Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__().
To create an object/class as an iterator you have to implement the methods __iter__() and __next__() to your object. As you have learned in the Python Classes/Objects chapter, all classes have a function called __init__(), which allows you do some initializing when the object is being created.
The __iter__() method acts similar, you can do operations (initializing etc.), but must always return the iterator object itself and the __next__() method also allows you to do operations, and must return the next item in the sequence.
To prevent the iteration to go on forever, we can use the StopIteration statement.
Currently we added Python Language Tutorials, more new updates coming soon.
We make a Collaboration with HackIsOn YouTube Channel So, Please Subscribe Their Channel and keep supporting