Explanation:
1. Creating the Set my_set
my_set = {1, 2, 3} creates a set with the elements {1, 2, 3}.
A set in Python is a collection of unique, unordered elements. Duplicate elements are not allowed.
2. Using the union Method
my_set.union({3, 4, 5}) combines all the unique elements from my_set and the set {3, 4, 5} into a new set.
The union() method does not modify the original set (my_set) but returns a new set containing the combined unique elements.
3. Combining Sets
my_set contains {1, 2, 3}.
The input to union is {3, 4, 5}.
The union of these two sets includes all unique elements:
{1, 2, 3, 4, 5}
Notice that 3 is present in both sets, but since sets do not allow duplicates, it appears only once.
4. Printing the Result
print(result) outputs:
{1, 2, 3, 4, 5}
The order of elements in the printed result may vary because sets in Python are unordered.
0 Comments:
Post a Comment