FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Set issubset() method with examples




The issubset() method in Python checks whether a given Set is a subset of another specified Set. If all the elements of a given Set is present in another Set then the given set is called the subset of another Set.
For example Lets say we have two sets A: {1, 2, 3} & B: {1, 2, 3, 4}, If we want to check whether A is a subset of B then we call this method like this - A.issubset(B), this method would return true because all the elements of set A are present in B which means A is a subset of B.

Python Set issubset() method Syntax

set.issubset(set)

Parameter: This method accepts a Set as a parameter.
Return Value: Boolean value. This method returns true or false based on the comparison, if the calling set is a subset of the set passed as a parameter then this method returns true else it returns false.

Python Set issubset() method Example

In the following example we have three set numbers and we are checking whether they are subset of each other using issubset() method. X is a subset of Y because all the elements of Set X are present in set Y, similarly Z is a subset of Y because all the elements of Set Z are present in Set Y.

# Set X
X = {1, 2, 3}

# Set Y
Y = {1, 2, 3, 4}

# Set Z
Z = {3, 4}

print("X is a subset of Y?", X.issubset(Y))
print("X is a subset of Z?", X.issubset(Z))
print("Y is a subset of X?", Y.issubset(X))
print("Z is a subset of Y?", Z.issubset(Y))






Leave Comment