ProgrammingVery hardPython · GATE DA · Advanced
What is printed?
What is printed?
a = list(range(6))
a[1:4] = 'xy'
print(a)
try:
a[::2] = [0, 0]
except ValueError as e:
print('VE')
a[2:2] = [7]
print(a, len(a))- A. [0, 'x', 'y', 4, 5] / [0, 0, 7, 0, 4, 5] 5
- B. [0, 'xy', 4, 5] / VE / [0, 'xy', 7, 4, 5] 5
- C. [0, 'x', 'y', 4, 5] / VE / [0, 'x', 7, 'y', 4, 5] 6
- D. [0, 'x', 'y', 4, 5] / VE / [0, 7, 'x', 'y', 4, 5] 6
Correct answer: C. [0, 'x', 'y', 4, 5] / VE / [0, 'x', 7, 'y', 4, 5] 6
A plain slice assignment iterates the right side (so 'xy' becomes two items) and may change the length; an extended slice (step 2) needs exactly matching length, here 3 slots vs 2 values, so ValueError. a[2:2] inserts.
More Python Programming questions
- What is printed? python a = [1, 2, 3] b = a b += [4] c = a c = c + [5] print(a, c)
- What is printed? python d = {'a': 1, 'b': 2} d['c'] = 3 del d['a'] d['a'] = 9 print(list(d.items()))
- What is printed? python s = "a,b,,c" print(s.split(','), s.split(',', 1), " x y ".split())
- What is printed? python print(list(zip('abc', range(5))), dict(zip('ab', [1, 2, 3])))
- What is printed? python def f(x, acc=[]): acc.append(x) return len(acc) print(f(1), f(2), f(3, []), f(4))
- What is printed? python print(all([]), any([]), all([0]), any([[]]), any([[0]]))