The sets are called disjoint if there are no common elements between the two sets. The python isdisjoint method will help us figure out whether the sets given are disjoint sets or not.
For example:
A = {1, 5, 9, 0, 2030, 78, 30120 }
B = {66, 88, 91032, 2, 4, -5}
A and B are disjoint sets as there are no common elements in both sets.
Syntax:
set_a.isdisjoint(set_b)
isdisjoint() Parameters
The python isdisjoint method takes one parameter as the input from the user. The parameter can either be a set, dictionary, string, tuple, or even a list. The python isdisjoint method converts the parameters given as input by the user into sets and checks whether there are any common elements.
Return Value from isdisjoint()
The isdisjoint() in python returns either of the two values:
-The method returns ‘true’ if the sets are disjoint. If the sets are not disjoint, false is returned.
Example 1: How isdisjoint() works?
A = {1, 2, 3, 4}
B = {5, 6, 7}
C = {4, 5, 6}
print('Are set A and set B disjoint?', A.isdisjoint(B))
print('Are set A and set C disjoint?', A.isdisjoint(C))
Output
Are set A and set B disjoint? True
Are set A and set C disjoint? False
Example 2: isdisjoint() with Other Iterables as arguments
A = {'a', 'b', 'z', 'c', 'd'}
B = ['b', 'e', 'f', 'k']
C = '5d2e411'
D ={1 : 'a', 2 : 'b', 3:'c'}
E ={'a' : 1, 'b' : 2}
print('Are set A and set B disjoint?', A.isdisjoint(B))
print('Are set A and set C disjoint?', A.isdisjoint(C))
print('Are set A and set D disjoint?', A.isdisjoint(D))
print('Are set A and set E disjoint?', A.isdisjoint(E))
Output
Are set A and set B disjoint? False
Are set A and set C disjoint? False
Are set A and set D disjoint? True
Are set A and set E disjoint? False
What is Isdisjoint in Python?
The python disjoint method checks whether two given sets are disjoint or not. If the sets are disjoint, then the statement is true else it is false.
Example:
x1 = {"apple", "strawberry ", "orange"}
y1 = {"google", "deloitte", "microsoft", "facebook"}
z1 = x1.isdisjoint(y1)
print(z1)
Output
True
Is Python a superset?
If a set contains all of the items of another set, the issuperset() method returns True (passed as an argument). If the sets do not contain all the items then it returns false.
Syntax:
A.issuperset(B)
Example: How issuperset() works?
P = {1, 2, 3, 4, 5}
Q = {1, 2, 3}
R = {1, 2, 3}
# Returns True
print(P.issuperset(Q))
# Returns False
print(Q.issuperset(P))
# Returns True
print(R.issuperset(Q))
Output
True
False
True
How do you intersect in Python?
The intersection is when there are one or more similar elements present inside the given sets. The python intersection method returns the set of elements that exists in both functions.
For example:
a1 = {"apple", "strawberry ", "orange"}
b1 = {"google", "deloitte", "microsoft", "apple"}
z1 = x1.intersection(y1)
print(z1)
Output:
{'apple'}
What is a subset in Python?
If all items of a set are present in another set (as arguments), the issubset() method returns True. If all the items of the set are not present in the other set, it returns false.
Here is how the issubset() works in python:
Source Code:
P= {1, 2, 3}
Q = {1, 2, 3, 4, 5}
R = {1, 2, 4, 5}
# Returns True
print(P.issubset(Q))
# Returns False as Q is not a subset of P
print(Q.issubset(P))
# Returns False as P is not a subset of R
print(P.issubset(R))
# Returns True
print(R.issubset(Q))
Output
True
False
False
True
Leave a Reply