Write a Python program to create a shallow copy of sets


This program demonstrates how to create a shallow copy of a set in Python. The code first declares two sets, a and b, containing strings. The sets are then compared to show that they contain the same elements.

Next, a shallow copy of set b is created and assigned to a using the copy() method. The copy() method returns a new set with a reference to the same elements as the original set. In other words, a shallow copy only copies the reference to the elements, not the actual elements themselves.

Finally, the contents of the new set a are printed to show that they are equal to the contents of set b. In this example, the sets a and b both contain the elements {"Tutoe", "Joes"}, and the shallow copy of set b assigned to a is also equal to set b.

Note : Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object

Source Code

a = {"Tutoe" , "Joes"}
b = {"Joes" , "Tutoe"}
#A shallow copy
a = b.copy()
print(a)
 

Output

{'Joes', 'Tutoe'}