ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
a = [1, 2, 3, 4]
a[1:3] = [9]
print(a)
b = [1, 2, 3, 4]
try:
b[::2] = [0]
except ValueError:
print('ValueError')- A. [1, 9, 4] | [0, 2, 4]
- B. [1, 9, 4] | ValueError
- C. [1, 9, 3, 4] | ValueError
- D. [1, 9] | ValueError
Correct answer: B. [1, 9, 4] | ValueError
Assigning to a plain slice may change the length, so [1, 2, 3, 4] becomes [1, 9, 4]. An extended slice with step 2 needs exactly two values, so the one-item assignment raises ValueError.
More Python Programming questions
- What is printed? python from collections import Counter def ways(a, t): dp = Counter({0: 1}) for x in a: nd = Counter(dp) for s, c in…
- What is printed? python print(bin(5 ^ 3), int('101', 2) << 2, ~5, -~5)
- What is printed? python import math print(math.prod([]), math.prod([2, 3.0]))
- What is printed? python import heapq h = [1, 3] heapq.heapify(h) print(heapq.heapreplace(h, 0), sorted(h), heapq.heappushpop(h, 0)…
- What is printed? python try: print(list(zip([1, 2], [3], strict=True))) except ValueError: print('strict')
- What is printed? python print(round(1234, -2), round(1250, -2), round(1350, -2))