Code Explanation:
1) def g1():
Defines a generator function g1.
When called, it will produce values 1 and 2, one at a time, using yield.
2) def g2():
Defines another generator g2.
Inside:
yield from g1() → delegates iteration to g1. This means g2 will yield everything g1 yields, in order.
After g1 finishes, it continues and yield 3.
3) print(list(g2()))
g2() creates a generator object.
list(g2()) consumes the generator fully, collecting all yielded values into a list.
Step-by-step execution:
yield from g1() → runs g1.
g1 yields 1 → collected.
g1 yields 2 → collected.
After g1 is done, g2 resumes.
yield 3 → collected.
So the list is: [1, 2, 3].
Final Output
[1, 2, 3]
Download Book - 500 Days Python Coding Challenges with Explanation
.png)

0 Comments:
Post a Comment