Disjoint() method of set in Python

Overview:

  • The method disjoint() determines whether two sets does not have any element in common.
  • The disjoint() method checks the disjoint property between two sets
  • In set theory, two sets are said to be disjoint if they do not have any element in common. When the intersection between two sets is an empty set, they are disjoint to each other. If A ∩ B = Ø then the sets A and B are called disjoint. 

Example:

# Example Python program that creates two sets
# and finds whether they are disjoint
woodwind = {"Piccolo", "Flute", "Oboe", "Clarinet", "Cor Anglais", 
            "Bassoon", "Contra Bassoon"}
            
guitars = {"Balalaika", "Mexican Mariachi Guitar",
           "Mandolin", "Ukulele", "Calssical guitar", "Electric Bass Guitar",
           "Electric Guitar"}

hasCommon = woodwind.isdisjoint(guitars)
print("Woodwinds and guitars have no instruments common to both:%s"%hasCommon)

Output:

Woodwinds and guitars have no instruments common to both:True

 


Copyright 2024 © pythontic.com