ProgrammingHardPython · GATE DA · Advanced
What is printed?
What is printed?
def gen():
x = yield 1
print('got', x)
y = yield x * 2
return y
g = gen()
print(next(g))
print(g.send(10))
try:
g.send(7)
except StopIteration as e:
print('stop', e.value)- A. 1 / got 10 / 20 / stop None
- B. 1 / got 10 / 10 / stop 7
- C. 1 / got 10 / 20 / stop 7
- D. 1 / got None / 20 / stop 7
Correct answer: C. 1 / got 10 / 20 / stop 7
The first next runs to the first yield (prints 1 only). send(10) makes x = 10, prints 'got 10' and yields 20. send(7) binds y = 7, and the return value 7 travels in StopIteration.value.
More Python Programming questions
- What is printed? python funcs = [] for i in range(3): funcs.append(lambda x=i: x 10) funcs.append(lambda: i 10) print([f() for f in funcs])
- What is printed? python class Tag: items = [] n = 0 def add(self, v): self.items += [v] self.n += 1 a, b = Tag(), Tag() a.add('x')…
- What is printed? python x = 10 class K: x = 20 ys = [x + i for i in range(2)] print(K.ys)
- What is printed? python import itertools data = [1, 1, 2, 1, 1, 1, 3] print([(k, len(list(g))) for k, g in itertools.groupby(data)])…
- What is printed? python class P: def init(self, v): self.v = v def eq(self, o): return self.v == o.v s = set() try: s.add(P(1)) except…
- What is printed? python def make(): n = 0 def inc(): n += 1 return n return inc try: print(make()()) except Exception as e…