In this Python set program, we will find the union of multiple sets by using the | operator with the help of the below-given steps.
What is set?
Sets are used to store multiple items in a single variable.
The set is one of 4 built-in data types in Python used to store collections of data.
It is an unordered collection data type that is iterable, mutable and has no duplicate elements.
Union of sets: Union is the combination of all the elements from more than one sets.
Union of multiple sets:
Steps to solve the program
1. Create multiple sets using {}.
2. Add some elements in the set.
3. Use “|” to find the union of sets using the “|” operator.
4. Print the output.
a = {1,2,4,5}
b = {7,8}
c={6,10,0}
print("Original set1: ",a)
print("Original set2: ",b)
print("Original set3: ",c)
print("Union of multiple sets using the | operator : ",a|b|c)
Output :
Original set1: {1, 2, 4, 5}
Original set2: {8, 7}
Original set3: {0, 10, 6}
Union of multiple sets using the | operator : {0, 1, 2, 4, 5, 6, 7, 8, 10}
Related Articles
Python program to find the symmetric difference of two sets using the “^” operator.
Python program to check if a set is a superset of another set.
Python program to find the common elements between two sets.
Python program to remove a specific element from a set.
Python program to add multiple elements to a set.
Python program to remove multiple elements from a set.