ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
print([x if x % 2 else -x for x in range(5)], [x for x in range(5) if x % 2 or x == 0])
- A. [1, 3] [0, 1, 3]
- B. [0, 1, -2, 3, -4] [1, 3]
- C. [0, 1, -2, 3, -4] [0, 1, 3]
- D. [-0, 1, -2, 3, -4] [0, 1, 3]
Correct answer: C. [0, 1, -2, 3, -4] [0, 1, 3]
A conditional expression BEFORE for transforms every element (odd x kept, even x negated; -0 is just 0). An if AFTER for filters: it keeps x when x is odd or x is 0, giving [0, 1, 3].
More Python Programming questions
- What is printed? python print(int(" -0012 "), int("1000"), float("1e3"), int("101", 2), int(-2.9))
- What is printed? python def g(): try: yield 1 yield 2 finally: print("cleanup", end=" ") it = g() print(next(it), end=" ") it.close()…
- What is printed? python data = [4, 7, 1, 9] best = 0 hi = [best := x for x in data if x > best] print(hi, best, "x" in globals())
- What is printed? python class M: def init(self, v): self.v = v def add(self, o): return M(self.v + (o.v if isinstance(o, M) else o)) ms =…
- What is printed? python print(f"{3.14159:.2f}|{42:>5}|{'ab':^6}|{255:#x}|{1234567:,}")
- What is printed? python grid = [[0, 0]] 2 copy1 = list(grid) copy2 = [row[:] for row in grid] grid[0][0] = 1 grid[1] = [5, 5] print(copy1…