How to Differentiate Between Python Set and Frozenset with Examples

This video provides a detailed explanation of the differences between Python’s set and frozenset data types. Through practical code demonstrations, we show the mutability of sets and how to manipulate their elements, as well as explain the immutability of frozensets and their operations. You’ll learn how to create, manipulate, and compare these two data types and their applications in different programming scenarios.

1. Video.

2. Python Source Code.

def mutable_set_example():
    my_set = {4, 5, 6, 4, 5}
    print("Original set:", my_set)
    
    # Add an element
    my_set.add(7)
    print("After adding an element:", my_set)
    
    # Remove an element
    my_set.remove(5)
    print("After removing an element:", my_set)
    
    # Union
    union_set = my_set.union({8, 9})
    print("Union:", union_set)
    
    # Intersection
    intersection_set = my_set.intersection({6, 7, 8})
    print("Intersection:", intersection_set)
    
    # Difference
    difference_set = my_set.difference({6, 7, 8})
    print("Difference:", difference_set)
    
    # Symmetric Difference
    symmetric_difference_set = my_set.symmetric_difference({6, 7, 8})
    print("Symmetric Difference:", symmetric_difference_set)
    
    # Update set
    my_set.update({8, 9})
    print("After update:", my_set)
    
    # Copy set
    copied_set = my_set.copy()
    print("Copied set:", copied_set)

def set_main():
    print("Mutable set example:")
    mutable_set_example()


def immutable_frozenset_example():
    my_frozenset = frozenset([4, 5, 6, 4, 5])
    print("Original frozenset:", my_frozenset)
    
    # Attempting to add or remove elements will raise an error
    try:
        my_frozenset.add(7)
    except AttributeError as e:
        print("Error when attempting to add an element:", e)
    
    try:
        my_frozenset.remove(5)
    except AttributeError as e:
        print("Error when attempting to remove an element:", e)
    
    # Union
    union_frozenset = my_frozenset.union([7, 8])
    print("Union:", union_frozenset)
    
    # Intersection
    intersection_frozenset = my_frozenset.intersection([5, 6, 7])
    print("Intersection:", intersection_frozenset)
    
    # Difference
    difference_frozenset = my_frozenset.difference([5, 6, 7])
    print("Difference:", difference_frozenset)
    
    # Symmetric Difference
    symmetric_difference_frozenset = my_frozenset.symmetric_difference([5, 6, 7])
    print("Symmetric Difference:", symmetric_difference_frozenset)
    
    # Copy frozenset
    copied_frozenset = my_frozenset.copy()
    print("Copied frozenset:", copied_frozenset)

def frozenset_main():
    print("Immutable frozenset example:")
    immutable_frozenset_example()


if __name__ == "__main__":
    set_main()

    frozenset_main()

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.