Beginner Level
Intermediate Level
Advanced Level
Introduction
In Python programming, a superset is a set that includes all the elements of another set. Python allows us to create supersets easily with its built-in set data type. Supersets can be useful in many scenarios, such as data manipulation, data analysis, and algorithmic problem-solving. In this tutorial, we will explore the concept of supersets in Python programming. We will cover how to define and create supersets, perform operations on supersets, and also demonstrate some common use cases where supersets can be particularly helpful. This tutorial assumes some prior knowledge of Python programming and set operations.
Table of Contents :
- What is a Superset
- using issuperset() method
- using the superset operator
- using the proper superset operator
What is a Superset :
- If all the elements of
set_2
are also present inset_1
thenset_1
is called a superset ofset_2
. - If
set_1
is not equal toset_2
thenset_1
is called a proper superset ofset_2
. - A set is also a superset of itself.
- In Python we can find if one set is a superset of another by using the :
- issuperset() method
- superset operator '>='
- proper superset operator '>'
using issuperset() method :
- This built-in method
issuperset()
can be used to check if a set is a superset of another or not. - The basic syntax of using the
issuperset()
method is :bool_value = set_1.issuperset(set_2)
- The
issuperset()
method returns a boolean value :- True if
set_1
is a superset ofset_2
- False if
set_1
is not a superset ofset_2
- True if
- Code Sample :
set_1 = {1, 2, 3}
set_2 = {1, 2, 3, 4, 5, 6, 7}
val = set_2.issuperset(set_1)
print(f"Value of set_1 = {set_1}")
print(f"Value of set_2 = {set_2}")
print(f"set_2 is superset of set_1 = {val}")
# Output
# Value of set_1 = {1, 2, 3}
# Value of set_2 = {1, 2, 3, 4, 5, 6, 7}
# set_2 is superset of set_1 = True
using the superset operator '>=' :
- The superset operator
>=
can be used to check if a set is a superset of another or not. - The basic syntax of using superset operator is :
set_1 >= set_2
- The superset operator returns a boolean value :
- True if
set_1
is a superset ofset_2
- False if
set_1
is not a superset ofset_2
- True if
- Code Sample :
set_1 = {1, 2, 3}
set_2 = {1, 2, 3, 4, 5, 6, 7}
val = set_2 >= set_1
print(f"Value of set_1 = {set_1}")
print(f"Value of set_2 = {set_2}")
print(f"set_2 is superset of set_1 = {val}")
# Output
# Value of set_1 = {1, 2, 3}
# Value of set_2 = {1, 2, 3, 4, 5, 6, 7}
# set_2 is superset of set_1 = True
using the proper superset operator '>' :
- The proper superset operator
>
can be used to check if a set is a proper superset of another or not. - The basic syntax of using proper superset operator is :
set_1 > set_2
- The proper superset operator returns a boolean value :
- True if
set_1
is a proper superset ofset_2
- False if
set_1
is not a proper superset ofset_2
- True if
- Code Sample :
set_1 = {1, 2, 3}
set_2 = {1, 2, 3, 4, 5, 6, 7}
val = set_2 > set_1
print(f"Value of set_1 = {set_1}")
print(f"Value of set_2 = {set_2}")
print(f"set_2 is proper superset of set_1 = {val}")
# Output
# Value of set_1 = {1, 2, 3}
# Value of set_2 = {1, 2, 3, 4, 5, 6, 7}
# set_2 is proper superset of set_1 = True
Prev. Tutorial : Subsets
Next Tutorial : Disjoint Sets