Sets

In Python, a set is a collection of unique and unordered elements, enclosed in curly braces {} or created using the set() constructor. Sets are mutable, which means you can add or remove elements from them.

Creating a Set:

To create a set, you can use the curly braces {} notation or the set() constructor. If you want to create an empty set, you must use the set() constructor because {} creates an empty dictionary.


            my_set = {1, 2, 3}
            print(my_set)  # Output: {1, 2, 3}

            empty_set = set()
            print(empty_set)  # Output: set()
        

Adding Elements:

You can add elements to a set using the add() method. If you try to add an element that already exists in the set, it will not be added again.


            my_set = {1, 2, 3}
            my_set.add(4)
            print(my_set)  # Output: {1, 2, 3, 4}
        

Removing Elements:

You can remove elements from a set using the remove() method. If you try to remove an element that does not exist in the set, it will raise a 'KeyError'.


            my_set = {1, 2, 3}
            my_set.add(4)
            print(my_set)  # Output: {1, 2, 3, 4}
        

Iterating Over Sets:

You can iterate over a set using a for loop, just like with lists and tuples. The loop will execute once for each unique element in the set.


            my_set = {1, 2, 3}
            for element in my_set:
                print(element)
        

In this example, we use a for loop to iterate over each element in the my_set set and print it to the console.

Set Operations

Sets support a variety of operations such as union, intersection, difference, and symmetric difference.

Union: returns a new set that contains all the unique elements from both sets.


            set1 = {1, 2, 3}
            set2 = {3, 4, 5}
            union_set = set1.union(set2)
            print(union_set)  # Output: {1, 2, 3, 4, 5}
        

Intersection: returns a new set that contains the elements that are common to both sets.


            set1 = {1, 2, 3}
            set2 = {3, 4, 5}
            intersection_set = set1.intersection(set2)
            print(intersection_set)  # Output: {3}
        

Difference: returns a new set that contains the elements from the first set that are not in the second set.


            set1 = {1, 2, 3}
            set2 = {3, 4, 5}
            difference_set = set1.difference(set2)
            print(difference_set)  # Output: {1, 2}
        

Symmetric Difference: returns a new set that contains the elements that are in one set or the other, but not both.


            set1 = {1, 2, 3}
            set2 = {3, 4, 5}
            symmetric_difference_set = set1.symmetric_difference(set2)
            print(symmetric_difference_set)  # Output: {1, 2, 4, 5}
        

Subsets and Supersets

In Python, sets support two methods for checking whether one set is a subset or superset of another set. These methods are called issubset() and issuperset(). Understanding subsets and supersets is important for working with sets in Python, as well as for solving problems that involve sets.

Subsets:

A subset is a set that contains all the elements of another set. In other words, if set A is a subset of set B, then every element in set A is also in set B. We use the symbol to indicate that set A is a subset of set B. For example, if set A contains the elements {1, 2, 3} and set B contains the elements {1, 2, 3, 4, 5}, then we can say that set A is a subset of set B, because every element in set A is also in set B.

In Python, we can use the issubset() method to check whether one set is a subset of another set. The issubset() method returns True if the set is a subset of the other set, and False otherwise. Here's an example:


            A = {1, 2, 3}
            B = {1, 2, 3, 4, 5}

            # check if A is a subset of B
            print(A.issubset(B)) # True
        

Supersets:

A superset is a set that contains all the elements of another set. In other words, if set A is a superset of set B, then every element in set B is also in set A. We use the symbol to indicate that set A is a superset of set B. For example, if set A contains the elements {1, 2, 3, 4, 5} and set B contains the elements {1, 2, 3}, then we can say that set A is a superset of set B, because every element in set B is also in set A.

In Python, we can use the issuperset() method to check whether one set is a superset of another set. The issuperset() method returns True if the set is a superset of the other set, and False otherwise. Here's an example:


            A = {1, 2, 3, 4, 5}
            B = {1, 2, 3}

            # check if A is a superset of B
            print(A.issuperset(B)) # True
        

Conclusion

Sets are a powerful data structure in Python for storing collections of unique and unordered elements. Sets are an important data structure in Python that can help you solve a variety of problems efficiently.