In Python, you can copy a set using the built-in set() constructor or by using the set.copy() method.
In this example,
numbers
containing some integer elements.numbers
to a new set named copied_numbers
using the set() constructor.copied_numbers
contains a copy of all elements from numbers
.numbers = {1, 2, 3, 4, 5}
copied_numbers = set(numbers)
print("Copied Numbers:", copied_numbers)
Copied Numbers: {1, 2, 3, 4, 5}
In this example,
fruits
containing some string elements.fruits
to a new set named copied_fruits
using the set.copy() method.copied_fruits
contains a copy of all elements from fruits
.fruits = {'Apple', 'Banana', 'Cherry', 'Date'}
copied_fruits = fruits.copy()
print("Copied Fruits:", copied_fruits)
Copied Fruits: {'Cherry', 'Apple', 'Banana', 'Date'}
In this tutorial, we learned How to Copy a Set in Python language with well detailed examples.