Menu
Coddy logo textTech

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: True

In 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: True

Here, 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: True

In 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: True

Here, 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 icon

Challenge

Easy

Create a function named check_sets that takes two sets, set1 and set2, as arguments. The function should perform the following operations:

  1. Check if set1 is a subset of set2.
  2. Check if set2 is a superset of set1.
  3. Check if set1 is a proper subset of set2.
  4. Check if set2 is a proper superset of set1.
  5. 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  # True

Check for proper subset using <:

set1 = {1, 2}
set2 = {1, 2, 3}
is_proper_subset = set1 < set2  # True

Check for superset using >= or issuperset():

set1 = {1, 2, 3}
set2 = {1, 2}
is_superset = set1 >= set2  # True

Check for proper superset using >:

set1 = {1, 2, 3}
set2 = {1, 2}
is_proper_superset = set1 > set2  # True

Try 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
    }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow