ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
i = 'x' _ = [i for i in range(3)] print(i)
- A. NameError
- B. x
- C. 2
- D. 0
Correct answer: B. x
In Python 3 a list comprehension has its own scope, so its loop variable does not overwrite the outer i. The outer i keeps the value 'x'.
More Python Programming questions
- What is printed? python print([y for x in range(5) if (y := x x) > 5])
- What is printed? python print(bin(10).count('1'), (10).bitcount(), (-10).bitcount())
- What is printed? python from collections import Counter print(Counter('abracadabra').mostcommon(3))
- What is printed? python print(float(10 16 + 1) == 10 16, 10 16 + 1 == float(10 16 + 1))
- What is printed? python x = 0 def f(): global x x += 1 return x print(f() + f() 10, x)
- What is printed? python print(0.1 + 0.2 == 0.3, round(0.1 + 0.2, 10) == 0.3, 3 0.1 == 0.3 - 0.2 + 0.1)