Write a Python program to create a union of sets


This program demonstrates the use of set unions in Python. Sets are unordered collections of unique elements, and set unions are used to combine the elements of two sets into a single set. The code first declares two sets, a and b, containing integers. The sets are then printed to show their original values.

Next, the code demonstrates how to find the union of the two sets, which is a set that contains all of the elements from both sets, with duplicates removed. The union of the two sets is found using the union() method, which takes one set as an argument and returns the union of the two sets. The result is stored in a new set, d, and its contents are then printed. In this example, the union of the sets a and b is the set {70, 20, 40, 50, 60, 30}, representing all of the unique elements from both sets combined.

Source Code

a = {30,40,70,20}
b = {20,50,60,40}
print("Original set Values :")
print(a)
print(b)
print("\nUnion of two Sets:")
d=a.union(b)
print(d)

Output

Original set Values :
{40, 20, 70, 30}
{40, 50, 20, 60}

Union of two Sets:
{70, 40, 50, 20, 60, 30}