ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
print(list(x for x in range(5) if x % 2 == 0)[1:3])
- A. [0, 2]
- B. [2, 4]
- C. [0, 2, 4]
- D. [2]
Correct answer: B. [2, 4]
The generator expression first yields the even values 0, 2, 4; converting to a list and then slicing [1:3] keeps the second and third elements, 2 and 4.
More Python Programming questions
- What is printed? python print({k: v for k, v in zip(['a', 'b', 'a'], [1, 2, 3])})
- What is printed? python matrix = [[1, 2, 3], [4, 5, 6]] print([[row[i] for row in matrix] for i in range(3)])
- What is printed? python print({x % 3 for x in range(10)})
- What is printed? python def countdown(n): while n > 0: yield n n -= 1 g = countdown(3) print(list(g)) print(list(g))
- What is printed? python it = iter([1, 2, 3]) print(next(it), next(it)) for x in it: print("loop", x) print(list(it))
- What is printed? python def flatten(lst): for item in lst: if isinstance(item, list): yield from flatten(item) else: yield item…