ProgrammingVery hardPython · GATE DA · Advanced
What is printed?
What is printed?
n = 1 xs = [1, 2, 3] g = (x for x in xs if x > n) xs = [10, 20] n = 2 print(list(g))
- A. []
- B. [3]
- C. [10, 20]
- D. [2, 3]
Correct answer: B. [3]
In a generator expression the outermost iterable is evaluated immediately (the original list), but the condition reads n lazily at iteration time, when n = 2, leaving only 3.
More Python Programming questions
- What is printed? python print(f"{255:#x} {255:08b} {3.14159:.2f} {'ab':>4}|{12345.678:,.1f}")
- What is printed? python def fib(n, memo={}): if n < 2: return n if n not in memo: memo[n] = fib(n - 1) + fib(n - 2) return memo[n]…
- What is printed? python class T: def init(self, n): self.n = n def enter(self): print('in', self.n, end=' ') return self def exit(self…
- What is printed? python a = [1, 2, 3] a[1:2] = a a.insert(-1, 'x') a.insert(99, 'y') print(a, a.pop(-2), a.index(3))
- What is printed? python print(True + True, sum([True, False, True]), isinstance(True, int), [10, 20, 30][True], 'ab' False == '')
- What is printed? python class Meta(type): def new(m, name, bases, d): d['tag'] = name.lower() return super().new(m, name, bases, d) class…