Explanation:
1. Creating the List
x = [1]
A list containing 1 is created.
x refers to this list.
x → [1]
2. Assigning y = x
y = x
This does not create a new list.
Both variables point to the same list:
x ──┐
↓
[1]
↑
y ──┘
So:
x is y
would already be True.
3. Using +=
x += [2]
For a list, += modifies the existing list in place.
The list changes from:
[1]
to:
[1, 2]
Because x and y refer to the same list, y also sees the change:
x ──┐
↓
[1, 2]
↑
y ──┘
4. Checking Identity
print(x is y)
The is operator checks whether two variables refer to the same object.
Here:
x → same list ← y
Therefore:
x is y → True
⚡ Complete Flow
x = [1]
↓
y = x
↓
Both refer to the same list
↓
x += [2]
↓
Same list becomes [1, 2]
↓
x is y
↓
True
✅ Final Output:

0 Comments:
Post a Comment