ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
def g():
    yield from range(2)
    yield from "ab"
    return "done"
def h():
    r = yield from g()
    yield r
print(list(h()), list(g()))
  1. A. [0, 1, 'a', 'b'] [0, 1, 'a', 'b']
  2. B. [0, 1, 'a', 'b', 'done'] [0, 1, 'a', 'b']
  3. C. [0, 1, 'a', 'b', 'done'] [0, 1, 'a', 'b', 'done']
  4. D. [0, 1, 'ab', 'done'] [0, 1, 'ab']

Correct answer: B. [0, 1, 'a', 'b', 'done'] [0, 1, 'a', 'b']

yield from works with any iterable (a string yields characters). A generator's return value is not yielded; it only becomes the value of a `yield from` expression, which h then yields explicitly.

More Python Programming questions

Exams that ask this topic