Write a Python program to check if two given sets have no elements in common


This program demonstrates how to check if two sets have any elements in common using the isdisjoint() method in Python. The code first declares three sets, a, b, and z, containing integers. The sets are then printed to show their original values.

Next, the isdisjoint() method is used to compare the sets and check if they have any elements in common. The isdisjoint() method returns True if the two sets have no elements in common and False if they do have elements in common.

In this example, the isdisjoint() method is used to compare the sets a and b, b and z, and a and z. The method returns True for all three comparisons, indicating that the sets have no elements in common.

Source Code

a = {23,45,78,8,56}
b = {42,55,26,87}
z = {87,46}
print("a :",a)
print("b :",b)
print("Z :",z)
print("\nTwo given sets have no Elements in Common..")
print("\nCompare A and B : ",a.isdisjoint(b))
print("Compare B and Z : ",b.isdisjoint(z))
print("Compare A and Z : ",z.isdisjoint(a))
 

Output

a : {23, 8, 56, 45, 78}
b : {42, 26, 55, 87}
Z : {46, 87}

Two given sets have no Elements in Common..

Compare A and B :  True
Compare B and Z :  False
Compare A and Z :  True