Question discussion

What is the output of the following Python code?g = (x * x for x in range(4)) print(sum(g), sum(g), list(g))

1388Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Python code?

g = (x * x for x in range(4))
print(sum(g), sum(g), list(g))
  • A14 14 [0, 1, 4, 9]
  • B14 0 []
  • C14 0 [0, 1, 4, 9]
  • D14 14 []
Correct answer: B

Explanation

A generator can be consumed only once; the first sum() exhausts it (0 + 1 + 4 + 9 = 14), so the second sum() gives 0 and list() gives an empty list.

Discussion 0

Nobody has replied yet. Start the discussion!