Write a Python program to Check if two sets have any elements in common. If yes, display the common elements


In this program, two sets s1 and s2 are created. The first set s1 contains the elements {1, 2, 3, 4, 5} and the second set s2 contains the elements {5, 3, 7, 8, 9}. The program then checks if the two sets have any elements in common using the isdisjoint() method. If the sets are disjoint, meaning they have no elements in common, the program will print "Two Sets No items in Common". If the sets are not disjoint, meaning they have common elements, the program will print "Two sets items in Common" and then print the common elements using the intersection() method.

Source Code

s1 = {1, 2, 3, 4, 5}
s2 = {5, 3, 7, 8, 9}
 
if s1.isdisjoint(s2):
  print("Two Sets No items in Common")
else:
  print("Two sets items in Common")
  print(s1.intersection(s2))

Output

Two sets items in Common
{3, 5}