Subsets and Supersets
Part of the Logic & Flow section of Coddy's Python journey — lesson 36 of 78.
In set theory, a subset is a set where all its elements are contained within another set. Conversely, a superset is a set that contains all the elements of another set.
Checking for a subset (<= or issubset()):
set1 = {1, 2}
set2 = {1, 2, 3}
is_subset = set1 <= set2
print(is_subset)
# Output: TrueIn this example, set1 is a subset of set2 because all elements of set1 are also in set2.
Checking for a proper subset (<):
set1 = {1, 2}
set2 = {1, 2, 3}
is_proper_subset = set1 < set2
print(is_proper_subset)
# Output: TrueHere, set1 is a proper subset of set2 because set1 is a subset of set2, and set2 contains at least one element not in set1.
Checking for a superset (>= or issuperset()):
set1 = {1, 2, 3}
set2 = {1, 2}
is_superset = set1 >= set2
print(is_superset)
# Output: TrueIn this case, set1 is a superset of set2 because set1 contains all elements of set2.
Checking for a proper superset (>):
set1 = {1, 2, 3}
set2 = {1, 2}
is_proper_superset = set1 > set2
print(is_proper_superset)
# Output: TrueHere, set1 is a proper superset of set2 because set1 is a superset of set2, and set1 contains at least one element not in set2.
Challenge
EasyCreate a function named check_sets that takes two sets, set1 and set2, as arguments. The function should perform the following operations:
- Check if
set1is a subset ofset2. - Check if
set2is a superset ofset1. - Check if
set1is a proper subset ofset2. - Check if
set2is a proper superset ofset1. - Return a dictionary containing the results of these operations, with the keys
"is_subset","is_superset","is_proper_subset", and"is_proper_superset".
Cheat sheet
A subset is a set where all its elements are contained within another set. A superset is a set that contains all the elements of another set.
Check for subset using <= or issubset():
set1 = {1, 2}
set2 = {1, 2, 3}
is_subset = set1 <= set2 # TrueCheck for proper subset using <:
set1 = {1, 2}
set2 = {1, 2, 3}
is_proper_subset = set1 < set2 # TrueCheck for superset using >= or issuperset():
set1 = {1, 2, 3}
set2 = {1, 2}
is_superset = set1 >= set2 # TrueCheck for proper superset using >:
set1 = {1, 2, 3}
set2 = {1, 2}
is_proper_superset = set1 > set2 # TrueTry it yourself
def check_sets(set1, set2):
# Check if set1 is a subset of set2
is_subset =
# Check if set2 is a superset of set1
is_superset =
# Check if set1 is a proper subset of set2
is_proper_subset =
# Check if set2 is a proper superset of set1
is_proper_superset =
# Return a dictionary containing the results
return {
"is_subset": is_subset,
"is_superset": is_superset,
"is_proper_subset": is_proper_subset,
"is_proper_superset": is_proper_superset
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Variables Exploration
ConstantsMultiple Variable AssignmentsSwapping VariablesPlaceholder VariablesRound NumbersList Casting4Contact Book Application
Display MenuAdd Contact7Sets Part 2
Mathematical Operations Part 1Mathematical Operations Part 2Recap - Treasure HuntSubsets and SupersetsIterating Over SetsRecap - Tournament Tracker2Dictionaries Part 1
What is a Dictionary?Creating a DictionaryAccessing ValuesModifying DictionariesRecap - Recipe Manager5Advanced Decision Making
Ternary OperatorMembership ChecksIdentity ChecksIndentation ErrorsRecap - Vacation Filter8Student Records Manager
Project OverviewAdd Student11Advanced Functions
Returning Multiple ValuesLambda Functions Part 1Lambda Functions Part 2Recap Challenge - Lambda SortRecursive Functions Part 1Recursive Functions Part 2Recap - Sum Nested List14Higher-Order Functions
The Map FunctionThe Filter FunctionRecap - Email ValidatorRecap - Number Processor3Dictionaries Part 2
Dictionary MethodsNested DictionariesChecking for KeysLooping Through DictionariesRecap - Frequency Counter9Advanced Data Aggregation
Using SumFinding Minimum and MaximumSorting Data EfficientlyRecap - Dictionary Sorter