Step-by-Step Explanation:
list1 = [1, 2, 3]:
A list [1, 2, 3] is created and assigned to list1.
list2 = list1:
list2 is assigned to reference the same list object as list1.
Both list1 and list2 now refer to the same object in memory.
list2.append(4):
The append(4) method modifies the list in place, adding the value 4 to the end of the list.
Since list1 and list2 refer to the same list, this change is reflected in both variables.
print(list1):
The list now contains [1, 2, 3, 4] because the modification affected the original list object.
Final Output:
[1, 2, 3, 4]
0 Comments:
Post a Comment