ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
data = [3, 8, 2, 9, 4]
if (n := len(data)) > 4:
print(n, end=" ")
print([y for x in data if (y := x * 2) > 10], y)- A. 5 [16, 18] 18
- B. [16, 18] 8
- C. 5 [16, 18] 8
- D. 5 [16, 18] NameError
Correct answer: C. 5 [16, 18] 8
The walrus inside the comprehension binds y in the enclosing (module) scope, and it is evaluated for every x, so y ends with the value from the last element: 4 * 2 = 8.
More Python Programming questions
- What is printed? python a = [5, 3, 5, 1] a.remove(5) a.insert(10, 7) a.insert(-10, 0) print(a, a.index(5), a.pop(1))
- What is printed? python a = [1, 2, 3, 4] b = a a = a.extend([5]) or a print(a is b, b[-1], [].extend([1]))
- What is printed? python s = "Mississippi" print(s.count("ss"), s.count("issi"), s.replace("ss", "S"), s.lower().index("s", 4))
- What is printed? python print("a" "b" 2, ("a" + "b") 2, "-".join(["a", "b"]) 2, "x".join("123"))
- What is printed? python s = "abc" print(repr(s[1:1] + s[3:] + s[-1:-2]), s[0:0] == s[5:9], len(s[-5:5]), s[True:])
- What is printed? python w = ["bob", "Al", "al", "Bo"] print(sorted(w), sorted(w, key=str.lower))