ProgrammingVery hardPython · GATE DA · Advanced
What is printed?
What is printed?
g = (x * 2 for x in range(3)) lst = [1, 2, 3] h = (y for y in lst if y > n) n = 1 lst = [5] print(list(g), list(h))
- A. [0, 2, 4] []
- B. [0, 2, 4] [5]
- C. [0, 2, 4] [1, 2, 3]
- D. [0, 2, 4] [2, 3]
Correct answer: D. [0, 2, 4] [2, 3]
The outermost iterable (lst) is evaluated immediately when the generator is created, so the old list [1, 2, 3] is used; the condition y > n is evaluated lazily, after n = 1, giving [2, 3].
More Python Programming questions
- What is printed? python class A: def new(cls, v): print('new', v) return super().new(B) if v else super().new(cls) def init(self, v)…
- What is printed? python d = {'a': 1, 'b': 2, 'c': 3} del d['a'] d['a'] = 0 d |= {'b': 9, 'z': 4} print(list(d.items()), min(d, key=d.get))
- What is printed? python x = [1, 2, 3, 4, 5, 6] for i in x: if i % 2 == 0: x.remove(i) d = {'a': 1} try: for k in d: d[k + 'x'] = 0 except…
- What is printed? python words = ['bb', 'a', 'cc', 'd', 'aa'] print(sorted(words, key=len, reverse=True)) print(sorted(words, key=lambda w…
- What is printed? python a = list(range(6)) a[1:4] = 'xy' print(a) try: a[::2] = [0, 0] except ValueError as e: print('VE') a[2:2] = [7]…
- What is printed? python a = [1, 2, 3] b = a b += [4] c = a c = c + [5] print(a, c)