w3resource

Python: Shallow copy of sets

Python sets: Exercise-11 with Solution

Write a Python program to create a shallow copy of sets.

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.

Visual Presentation:

NumPy Sets: Shallow copy of sets.

Sample Solution:

Python Code:

# Create a set 'setp' with elements "Red" and "Green".
setp = set(["Red", "Green"])

# Create a set 'setq' with elements "Green" and "Red".
setq = set(["Green", "Red"])

# Create a shallow copy of 'setp' and store it in 'setr'.
setr = setp.copy()

# Print the contents of the shallow copy 'setr'.
print(setr) 

Sample Output:

{'Red', 'Green'}

Python Code Editor:

Previous: Write a Python program to check if a set is a subset of another set.
Next: Write a Python program to remove all elements from a given set.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/sets/python-sets-exercise-11.php