Code Explanation:
๐น Step 1: Create List a
a = [1, 2]
A list a is created
Memory: a → [1, 2]
๐น Step 2: Copy List using Slicing
b = a[:]
a[:] creates a shallow copy of the list
New list is created in memory
๐ Now:
a → [1, 2]
b → [1, 2] (different object)
๐น Step 3: Modify b
b[0] = 9
Only list b is changed
a remains unchanged
๐ Now:
a → [1, 2]
b → [9, 2]
๐น Step 4: Print Output
print(a, b)
๐ Output:
[1, 2] [9, 2]

0 Comments:
Post a Comment