Step-by-step explanation:
-
a = [10, 20, 30]
→ Creates a list in memory: [10, 20, 30]. -
b = a
→ b does not copy the list.
It points to the same memory location as a.
So both a and b refer to the same list. -
b += [40]
→ The operator += on lists means in-place modification (same as b.extend([40])).
It adds 40 to the same list in memory. -
Since a and b share the same list,
when you modify b, a also reflects the change.
✅ Output:
[10, 20, 30, 40]
Key Concept:
b = a → same object (shared reference)
b = a[:] or b = a.copy() → new list (independent copy)


0 Comments:
Post a Comment