Python set Method - isdisjoint()

The isdisjoint() method in Python is used to check if two sets have any common elements. It returns True if the sets are disjoint (i.e., have no common elements), and False otherwise.

The syntax for the isdisjoint() method is as follows:

r‮‬efer to:theitroad.com
set1.isdisjoint(set2)

Here, set1 and set2 are the sets that we want to check for common elements. The method returns True if the sets are disjoint (i.e., have no common elements), and False otherwise.

Example:

# Creating two sets
set1 = {1, 2, 3}
set2 = {4, 5, 6}

# Checking if the sets are disjoint
print(set1.isdisjoint(set2))   # Output: True

# Creating another set with some common elements
set3 = {3, 4, 5}

# Checking if the sets are disjoint
print(set1.isdisjoint(set3))   # Output: False

In the above example, the isdisjoint() method is used to check if the sets set1 and set2 are disjoint. Since they have no common elements, the method returns True. The method is also used to check if the sets set1 and set3 are disjoint. Since they have the common element 3, the method returns False.