Question discussion

What is the output of the following Python code?def g(): for i in range(3): yield i * i print(sum(g()), list(g())[-1])

1120Medium DSSSB TGT Computer Science 0 replies

What is the output of the following Python code?

def g():
    for i in range(3):
        yield i * i
print(sum(g()), list(g())[-1])
  • A5 2
  • B3 4
  • C5 4
  • D14 4
Correct answer: C

Explanation

The generator yields 0, 1, 4: the sum is 5 and the last element is 4.

Discussion 0

Nobody has replied yet. Start the discussion!