In Python, an iterator is an object that implements the iterator protocol, which consists of the `__next__()` and `__iter__()` methods. The `__next__()` method returns the next value in the sequence, and the `__iter__()` method returns the iterator object itself.
You can create an iterator in Python by defining a class that implements the iterator protocol. Here’s an example:
python class MyIterator: def __init__(self, start, end): self.current = start self.end = end def __iter__(self): return self def __next__(self): if self.current < self.end: value = self.current self.current += 1 return value else: raise StopIteration # Use the iterator to generate a sequence of values my_iterator = MyIterator(0, 5) for value in my_iterator: print(value)
In this example, we define an iterator class called `MyIterator` that takes two arguments `start` and `end`. The `__iter__()` method returns the iterator object itself, and the `__next__()` method generates the next value in the sequence until we reach the end, at which point it raises a `StopIteration` exception.
We then create an instance of the iterator class called `my_iterator`, and use it in a `for` loop to generate a sequence of values and print them one at a time.
In addition to creating iterators using custom classes, Python provides several built-in functions and objects that implement the iterator protocol, including `range()`, `zip()`, and `map()`. For example, you can use the `range()` function to generate a sequence of integers:
python # Use the built-in range() function as an iterator my_iterator = iter(range(5)) for value in my_iterator: print(value)
In this example, we use the `iter()` function to convert the output of the `range()` function into an iterator, and then use it in a `for` loop to generate a sequence of integers and print them one at a time.
Overall, iterators are a powerful tool for generating sequences of values on-the-fly, and are used extensively throughout Python's standard library and ecosystem.