Explanation:
1. Creating List x
x = [1,2]
A list containing 1 and 2 is created.
x stores the reference (address) of that list in memory.
Memory concept:
x ───► [1, 2]
2. Assigning y = x
y = x
y does not create a new list.
y points to the same list as x.
Now both variables reference the same object.
Memory concept:
x ───► [1, 2]
y ───► [1, 2]
3. Clearing the List
x.clear()
.clear() removes all elements from the existing list.
Since x and y point to the same list, the change is visible through both variables.
After clearing:
x ───► []
y ───► []
4. Printing y
print(y)
y points to the same cleared list.
So the output becomes:
[]
Final Output:

0 Comments:
Post a Comment