Explanation:
๐น Step 1: Initialize the List
x = [1,2,3]
A list x is created
๐ Current value:
[1, 2, 3]
๐น Step 2: Insert Element
x.insert(1,9)
insert(index, value) places the value at the given index
Elements at and after that index shift right
๐ Insert 9 at index 1
Before:
[1, 2, 3]
After:
[1, 9, 2, 3]
๐น Step 3: Apply Slicing
x[1:3]
Slice from index 1 to 3 (3 is excluded)
๐ From [1, 9, 2, 3]:
Index 1 → 9
Index 2 → 2
๐ Result:
[9, 2]
๐น Step 4: Print Output
print(x[1:3])
๐ Final Output:
[9, 2]

0 Comments:
Post a Comment