Python set Method - issubset()
The issubset()
method in Python is used to check if a set is a subset of another set. It returns True
if all the elements of the first set are also present in the second set, and False
otherwise.
The syntax for the issubset()
method is as follows:
set1.issubset(set2)Sww:ecruow.theitroad.com
Here, set1
is the set that we want to check if it is a subset of set2
. The method returns True
if all the elements of set1
are also present in set2
, and False
otherwise.
Example:
# Creating two sets set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5} # Checking if set1 is a subset of set2 print(set1.issubset(set2)) # Output: True # Creating another set set3 = {4, 5, 6} # Checking if set1 is a subset of set3 print(set1.issubset(set3)) # Output: False
In the above example, the issubset()
method is used to check if the set set1
is a subset of set2
. Since all the elements of set1
(i.e., 1
, 2
, and 3
) are also present in set2
, the method returns True
. The method is also used to check if the set set1
is a subset of set3
. Since the element 3
is not present in set3
, the method returns False
.