Python Set isdisjoint() Method (with Examples)

Python Set isdisjoint() Method: In this tutorial, we will learn about the isdisjoint() method of the set class with its usage, syntax, parameters, return type, and examples. By IncludeHelp Last updated : June 14, 2023

Python Set isdisjoint() Method

The isdisjoint() is an inbuilt method of the set class that is used to check whether elements are common in two sets or not, it is called with one set (commonly known as this set), and another set is supplied as an argument, the method returns "True" if none of the elements are common in both sets, else it returns "False".

Syntax

The following is the syntax of isdisjoint() method:

set1.isdisjoint(set2)

Parameter(s):

The following are the parameter(s):

  • set1 – It represents the set1 (this set).
  • set2 – It represents the set2 (another set to be compared).

Return Value

The return type of this method is <class 'bool'>, it returns a Boolean value (True or False).

Example 1: Use of Set isdisjoint() Method

# declaring the sets
cars_1 = {"Porsche", "Audi", "Lexus"}
cars_2 = {"Porsche", "Mazda", "Lincoln"}
cars_3 = {"BMW", "Mazda", "Lincoln"}

# isdisjoint() method call
result = cars_1.isdisjoint(cars_2)
print("cars_1.isdisjoint(cars_2): ", result)

result = cars_2.isdisjoint(cars_3)
print("cars_2.isdisjoint(cars_3): ", result)

result = cars_1.isdisjoint(cars_3)
print("cars_1.isdisjoint(cars_3): ", result)

Output

cars_1.isdisjoint(cars_2):  False
cars_2.isdisjoint(cars_3):  False
cars_1.isdisjoint(cars_3):  True

Example 2: Use of Set isdisjoint() Method

# declaring the sets
x = {"ABC", "PQR", "XYZ"}
y = {"ABC", "PQR", "XYZ"}
z = {"DEF", "MNO", "UVW"}

# isdisjoint() method calls
result = x.isdisjoint(y)
print("x.isdisjoint(y): ", result)

result = y.isdisjoint(z)
print("y.isdisjoint(z): ", result)

result = z.isdisjoint(x)
print("z.isdisjoint(x): ", result)

result = x.isdisjoint(z)
print("x.isdisjoint(z): ", result)

result = y.isdisjoint(x)
print("y.isdisjoint(x): ", result)

Output

x.isdisjoint(y):  False
y.isdisjoint(z):  True
z.isdisjoint(x):  True
x.isdisjoint(z):  True
y.isdisjoint(x):  False


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.