Python: Shallow copy of sets
11. Create a Shallow Copy of a Set
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:
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'}
For more Practice: Solve these Related Problems:
- Write a Python program to create a shallow copy of a set using the copy() method.
- Write a Python program to create a duplicate of a set by converting it to a set again.
- Write a Python program to implement a function that returns a shallow copy of a given set.
- Write a Python program to demonstrate the difference between a shallow copy and a reference copy of a set.
Go to:
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.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.