ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
a = list(range(6))
try:
a[::2] = [9, 9]
except ValueError as e:
print("VE", end=" ")
a[1:2] = []
a[:0] = [7, 7]
print(a)- A. [9, 1, 9, 3, 5]
- B. VE [7, 7, 0, 1, 2, 3, 4, 5]
- C. VE [7, 7, 0, 2, 3, 4, 5]
- D. VE [0, 2, 3, 4, 5, 7, 7]
Correct answer: C. VE [7, 7, 0, 2, 3, 4, 5]
An extended slice (step 2) selects 3 items, so assigning a 2-item list raises ValueError and changes nothing. Then a[1:2] = [] deletes 1, and a[:0] = [...] inserts at the front.
More Python Programming questions
- What is printed? python a = [0, 1, 2, 3, 4, 5, 6, 7] del a[::3] print(a, a[-1:0:-2])
- What is printed? python s = slice(1, None, 2) t = "abcdefg" print(t[s], s.indices(len(t)), t[::-1][s])
- What is printed? python a = [1, 2, 3, 4, 5] a[1:4] = "xy" b = a[:] b[::-1] = b print(a, b[0])
- What is printed? python nums = [10, 20, 30, 40, 50] print(nums[-2::-2], nums[1:-1][::-1], nums[::-1][1:-1])
- What is printed? python s = "abcdef" print(s[5:1:-1], s[-1:-7:-1] == s[::-1], s[:-7:-1], s[2::-1])
- What is printed? python d = {"a": 1, "b": 2, "c": 3} d["a"] = 10 del d["b"] d["b"] = 20 d.update({"a": 0, "z": 9}) print(list(d)…