set.copy() returns a shallow copy of a set. The returned set is a different container, so adding or removing elements from the copy does not change the original set.
This is useful when you want to experiment with mutable set operations while preserving the original collection.
my_set={'a','b','c','d','e'}
my_set2=my_set.copy()
print(my_set2)
Output is here
{'d', 'a', 'b', 'c', 'e'}
original_set = {1, 2, 3}
copied_set = original_set.copy()
copied_set.add(4)
print(copied_set) # Output: {1, 2, 3, 4}
print(original_set) # Output: {1, 2, 3}
set1 = {'apple', 'banana'}
set2 = set1.copy()
set2.remove('banana')
print(set2) # Output: {'apple'}
All Set Methods Questions with solutions on set discard()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.