Step-by-Step Explanation
-
Create list a:
-
A list a is created with elements 1, 2, and 3.
-
-
Create list b using slicing:
-
This creates a shallow copy of list a.
b now holds a separate copy of the list [1, 2, 3].
-
So now:
-
a = [1, 2, 3]
- b = [1, 2, 3]
-
-
Modify b using .append(4):
-
The number 4 is added only to list b, not a.
-
Now:
a = [1, 2, 3] (unchanged)
b = [1, 2, 3, 4] (modified)
-
-
Print list a:
-
This prints:
-
✅ Output:
Key Concept:
a[:] creates a new list object (a shallow copy).
-
Changes made to b do not affect a.
-
If you had written b = a instead, then a would have been affected.


0 Comments:
Post a Comment