ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
from functools import partial
def p(a, b):
return a * 10 + b
fs = [partial(p, i) for i in range(3)]
gs = [lambda b: p(i, b) for i in range(3)]
print(fs[0](5), gs[0](5))- A. 5 5
- B. 25 25
- C. 25 5
- D. 5 25
Correct answer: D. 5 25
partial binds the current value of i at creation (0), giving 0*10+5 = 5. The lambda looks i up late, after the comprehension finished with i = 2, giving 2*10+5 = 25.
More Python Programming questions
- What is printed? python def counter(): count = 0 def inc(): nonlocal count count += 1 return count def peek(): return count return inc…
- What is printed? python x = 0 def outer(): x = 1 def mid(): def inner(): nonlocal x x += 10 return x return inner() r = mid() return r, x…
- What is printed? python def f(): try: print(y, end=" ") except NameError as e: print(type(e).name, end=" ") y = 1 y = 5 f() print(y)
- What is printed? python def gen(): for i in range(3): yield lambda: i a = [f() for f in gen()] b = [f() for f in list(gen())] print(a, b)
- What is printed? python x = 1 g = (x y for y in range(3)) x = 2 print(list(g))
- What is printed? python xs = [1, 2, 3] g = (a 10 + b for a in xs for b in xs) xs = [10, 20] print(sum(g))