Set Methods
copy()
Return a shallow copy of the set.
set.copy()
set.copy() creates a new set with a duplicate of all elements from the original set.
Adding or removing elements in the copy does not alter the original set.
Result
Returns
A new shallow copy of the set.
Run it
Try it
Run it and change it
original = {"alpha", "beta"}
backup = original.copy()
backup.add("gamma")
print("Original:", sorted(original))
print("Backup:", sorted(backup)) Output
Press Run to see what it prints.Worth knowing
- Alternative syntax:
set(original)produces an identical shallow copy.