Code Explanation:
๐ Heading 1: Create the Original List
a = [1, 2]
Explanation
Creates a list named a.
The list contains two elements: 1 and 2.
๐ Heading 2: Create a Copy of the List
b = a.copy()
Explanation
copy() creates a new list with the same elements.
b is independent of a.
Both lists have identical values but different memory locations.
๐ Heading 3: Add a New Element
b.append(3)
Explanation
append() adds the value 3 to the end of list b.
Only b changes because it is a separate copy.
The original list a remains unchanged.
๐ Heading 4: Print the Original List
print(a)
Explanation
Prints the contents of the original list a.
Since only b was modified, a still contains its original values.
๐ Output
[1, 2]

0 Comments:
Post a Comment