Explanation:
๐น Step 1: Create Generator
x = (i for i in range(4))
This creates a generator object
Values inside generator:
0, 1, 2, 3
⚠️ Important:
Generator values are produced one by one
Once used → they disappear ๐
๐น Step 2: Execute sum(x)
sum(x)
Python starts consuming generator:
0 + 1 + 2 + 3 = 6
๐ Result:
6
๐น Step 3: Generator Gets Exhausted
After sum(x):
x → empty generator
⚠️ All values already consumed
Generator now has:
nothing left
๐น Step 4: Execute list(x)
list(x)
But generator already empty ❗
So:
[]
๐น Step 5: Print Final Output
print(6, [])
๐ Final Output:
6 []

0 Comments:
Post a Comment