Explanation:
Initialization:
arr = [1, 2, 3, 4] → we start with a list of 4 elements.
Slice assignment:
arr[1:3] → selects the elements from index 1 up to (but not including) index 3.
That is, [2, 3].
arr[1:3] = [8, 9] → replaces the selected slice [2, 3] with the new list [8, 9].
Step-by-step update:
Original array: [1, 2, 3, 4]
Replace arr[1:3] → [2, 3] becomes [8, 9]
New array: [1, 8, 9, 4]
Printing:
print(arr) → outputs the updated list: [1, 8, 9, 4].
Final Output:
[1, 8, 9, 4]
Key Concept:
In Python, slice assignment lets you replace multiple elements in a list at once, and the new list does not need to be the same length as the slice.
.png)

0 Comments:
Post a Comment