ProgrammingHardAccenture pattern
What is printed?
What is printed?
print([x if x % 2 == 0 else -x for x in range(5)])
- A. [0, 1, 2, 3, 4]
- B. [0, -1, 2, -3, 4]
- C. [-0, -1, -2, -3, -4]
- D. [0, -1, -2, -3, 4]
Correct answer: B. [0, -1, 2, -3, 4]
This is a conditional expression INSIDE the comprehension's output (not a filter): even x values pass through unchanged, odd x values are negated: 0, -1, 2, -3, 4.
More Python Programming questions
- What is printed? python words = ["banana", "kiwi", "apple", "fig"] print(sorted(words, key=len)) print(sorted(words, key=lambda w: w[-1]))
- What is printed? python s = "Hello World" print(s.swapcase()) print(s.title()) print(" a b ".split())
- What is printed? python print("a-b--c".split("-")) print("a-b--c".split("-", 1))
- What is printed? python d = {"b": 2, "a": 1, "c": 3} print(sorted(d.items())) print(dict(sorted(d.items())))
- What is printed? python from functools import lrucache calls = [] @lrucache(maxsize=None) def f(n): calls.append(n) return n n f(2); f(3)…
- What is printed? python def deco(f): def wrapper(a, k): wrapper.calls += 1 return f(a, k) wrapper.calls = 0 return wrapper @deco def h()…