Question discussion

What is the output of the following Python code?a = [1, 2, 3, 4, 5] b = a[::-2] a[1:3] = [9] print(b, a)

962Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Python code?

a = [1, 2, 3, 4, 5]
b = a[::-2]
a[1:3] = [9]
print(b, a)
  • A[5, 3, 1] [1, 2, 9, 4, 5]
  • B[5, 3, 1] [1, 9, 4, 5]
  • C[1, 3, 5] [1, 9, 4, 5]
  • D[5, 3, 1] [1, 9, 3, 4, 5]
Correct answer: B

Explanation

a[::-2] gives [5, 3, 1] and slice assignment replaces the two items at indices 1–2 with the single item 9.

Discussion 0

Nobody has replied yet. Start the discussion!