Explanation
✅ a = (i for i in range(3))
This is a generator expression.
It creates a generator that will yield values one by one from range(3) → 0, 1, 2.
At this point, nothing is printed. The generator is lazy, meaning it does not compute values until requested.
✅ print(next(a))
This calls next() on the generator a.
The first value yielded from the range is 0.
So it prints:
✅ print(next(a))
Now, the generator moves to the next value, which is 1.
So it prints:
✅ Final Output
Note:
If you call next(a) again, it would print 2. After that, another next(a) would raise a StopIteration error because there are no more items in the generator.


0 Comments:
Post a Comment