Code Explanation:
1. Creating a generator expression
x = (i*i for i in range(3))
Explanation:
Creates a generator x that will yield squares of numbers 0, 1, and 2 (i.e., 0, 1, 4).
The generator is lazy — it doesn’t calculate anything until you ask for values.
Output:
None
2. Getting the first value from the generator
print(next(x))
Explanation:
Calls next(x) to get the first value from the generator.
3. Getting the second value from the generator
print(next(x))
Explanation:
Calls next(x) again.
4. Recreating the generator expression (resetting)
x = (i*i for i in range(3))
Explanation:
Assigns a new generator to x.
This resets the sequence back to start from 0 again.
Output:
None
5. Getting the first value from the new generator
print(next(x))
Explanation:
Calls next(x) on the new generator.
Final Output:
0
1
0
.png)

0 Comments:
Post a Comment