Explanation:
✅ Line 1: Create a List
a = [1, 2]
Explanation
A new list object is created in memory.
Variable a stores the reference (address) of that list.
Memory
a ─────► [1, 2]
✅ Line 2: Assign Another Variable
b = a
Explanation
No new list is created.
b simply points to the same list as a.
Memory
a ──┐
├────► [1, 2]
b ──┘
Both variables refer to the same object.
✅ Line 3: Create a New List Using +
a = a + [3]
Explanation
This line is not modifying the existing list.
Instead,
Python creates a new list by combining:
[1, 2] + [3]
Result:
[1, 2, 3]
Variable a is updated to point to this new list.
The original list remains unchanged.
Memory Before
a ──┐
├────► [1, 2]
b ──┘
Memory After
a ─────► [1, 2, 3]
b ─────► [1, 2]
Notice that b is still pointing to the original list.
✅ Line 4: Print
print(b)
Explanation
b still references the original list.
So Python prints:
[1, 2]
✅ Final Output
[1, 2]

0 Comments:
Post a Comment