Explanation:
๐น Step 1: Create Generator
x = (i*i for i in range(3))
This creates a generator object
It does NOT store values immediately
It will generate values on demand (lazy evaluation)
๐ Values it can produce:
0, 1, 4
๐น Step 2: First sum(x)
sum(x)
Generator starts running:
0*0 = 0
1*1 = 1
2*2 = 4
๐ Total:
0 + 1 + 4 = 5
After this, generator is fully consumed (exhausted) ❗
๐น Step 3: Second sum(x)
sum(x)
Generator has no values left
It is already exhausted
๐ So:
sum = 0
๐น Step 4: Final Print
print(sum(x), sum(x))
๐ Output:
5 0

0 Comments:
Post a Comment