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.



Follow us on Facebook and Twitter for latest update.