Find the union, symmetric difference, and intersection of the two sets. Print the results of each operation


The program is an implementation of set operations in Python. It creates two sets, s and val, and performs set operations like union, symmetric difference, and intersection on them.

  • s is created as an empty set and the elements in the range of 5 to 15 are added to it using the update() method.
  • val is created as a set with values {11, ("Python", "C"), ('J','O','E'), 10, 20}.
  • The union of two sets is obtained using the union() method and is stored in a new set. The union of two sets contains all the elements of both sets, and the duplicate values are eliminated.
  • The symmetric difference of two sets is obtained using the symmetric_difference() method and is stored in a new set. The symmetric difference of two sets contains the elements that are present in either of the sets but not in both.
  • The intersection of two sets is obtained using the intersection() method and is stored in a new set. The intersection of two sets contains the elements that are common in both sets.

Finally, all the sets are printed to show the result of set operations.

Source Code

s = set()
s.update(range(5, 15))
 
val = { 11, ("Python", "C"), ('J','O','E'),10 , 20 }
print("Set 1 :",s)
print("\nSet 2 :",val)
print("Union : ",s.union(val))
print("Symmetric Difference :",s.symmetric_difference(val))
print("Intersection :",s.intersection(val))

Output

Set 1 : {5, 6, 7, 8, 9, 10, 11, 12, 13, 14}

Set 2 : {20, ('Python', 'C'), 10, 11, ('J', 'O', 'E')}
Union :  {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ('Python', 'C'), 20, ('J', 'O', 'E')}
Symmetric Difference : {5, 6, 7, 8, 9, ('Python', 'C'), 12, 13, 14, 20, ('J', 'O', 'E')}
Intersection : {10, 11}