Step-by-Step Explanation:
-
x = [1, 2, 3]
-
A list x is created.
-
Lists are mutable in Python.
-
-
modify(x)
-
You pass the list x to the function modify.
-
The parameter a now refers to the same list as x (initially).
-
-
Inside the function:
-
a = a + [4, 5]
-
This creates a new list by combining a and [4, 5].
-
The result [1, 2, 3, 4, 5] is assigned to a, but this does not affect the original list x.
a now refers to a new list, but x still points to the original [1, 2, 3].
-
-
a = a + [4, 5]
-
After the function call:
x is unchanged.
-
So print(x) outputs:
Why didn't the original list change?
Because this line:
does not modify the list in-place. It creates a new list and reassigns it to a, breaking the reference to the original list x.
✅ To modify the original list, you'd use:
Or:
These modify the list in-place, so x would then change to [1, 2, 3, 4, 5].

.png)
.png)



.png)
.png)


.png)
.png)
