Code :
num = [10, 20, 30, 40, 50]
num[1:4] = [15, 25, 35]
print(num)
Solution and Explanation :
The given code creates a list called num with elements [10, 20, 30, 40, 50]. Then, it uses slicing to replace elements at indices 1 to 3 (excluding 4) with the values [15, 25, 35]. Finally, it prints the modified list.
Let's break it down:
num = [10, 20, 30, 40, 50]
This creates a list with the elements 10, 20, 30, 40, and 50.
num[1:4] = [15, 25, 35]
This line uses slicing to replace the elements at indices 1 to 3 with the values 15, 25, and 35. So, the list becomes [10, 15, 25, 35, 50].
print(num)
This prints the modified list:
[10, 15, 25, 35, 50]
0 Comments:
Post a Comment