Class - 12 CBSE Computer Science Important Output Questions 2025
Predict the output of the following code snippet ?
a = [1, 2, 3, 4, 5]
print(a[3:0:-1])
Python
Python List Manipulation
2 Likes
Answer
[4, 3, 2]
Working
The slicing notation a[start:stop:step]
extracts a portion of the list from index start
to stop-1
with a specified step. In the slicing part a[3:0:-1]
:
start
is 3, which corresponds to the element with value 4.stop
is 0, but as element at stop index is excluded so slicing goes up to index 1.step
is -1, indicating that we want to step backward through the list.
Putting it together:
a[3:0:-1]
This extracts elements from index 3 to (0+1) in reverse order with a step of -1.
The output of the code will be:
[4, 3, 2]
Answered By
1 Like