Intermediate
Write an iterator class
Write a class Evens whose constructor takes a limit. Looping over Evens(limit) produces the even numbers from 0 up to but not including limit.
It must be a real iterator: write __iter__ (returning self) and __next__ (raising StopIteration at the end). Then print list(Evens(7)).
What Check tests
list(Evens(7))is[0, 2, 4, 6]Evens(0)is empty andEvens(1)is[0]iter(e)returns the same object- It is used up after one pass
nextworks and ends with StopIteration- The program prints
[0, 2, 4, 6]
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.