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()))- A. [0, 1, 'a', 'b'] [0, 1, 'a', 'b']
- B. [0, 1, 'a', 'b', 'done'] [0, 1, 'a', 'b']
- C. [0, 1, 'a', 'b', 'done'] [0, 1, 'a', 'b', 'done']
- 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
- What is printed? python def evens(): n = 0 while True: yield n n += 2 def take(k, it): return [next(it) for in range(k)] e = evens()…
- What is printed? python def pipeline(): nums = (n for n in range(10)) sq = (n n for n in nums if n % 2) big = (s for s in sq if s > 10)…
- What is printed? python def g(): print("a", end=" ") x = yield print("b", x, end=" ") yield x 2 it = g() print(it.send(None), end=" ")…
- What is printed? python def g(): try: yield 1 except GeneratorExit: print("exit", end=" ") yield 2 it = g() next(it) try: it.close()…
- What is printed? (Output lines are shown separated by " / ".) python it = iter([1, 2, 3, 4, 5, 6]) print([(a, b) for a, b in zip(it, it)]…
- What is printed? python s = {1, 2, 3} t = {3, 4} print(s | t, s & t, s - t, s ^ t, s >= {1, 2})