Python Sets

Sets operations and modification in Python.

Python - Sets:

A set is an unordered collection of unique elements. Sets are mutable, which means you can modify them after creation. Sets are denoted by curly braces '{}', and elements are separated by commas.


Creating Sets:

Sets can be created using curly braces '{}' or the set '()' function:

# Creating a set using curly braces
my_set = {1, 2, 3, 4, 5}

# Creating a set using the set() function
another_set = set([6, 7, 8, 9, 10])

Operations on Sets:

Sets support various operations like union, intersection, difference, and symmetric difference:

Union ('|'): Combines elements from two sets.

Intersection ('&'): Returns elements common to both sets.

Difference ('-'): Returns elements in the first set but not in the second set.

Symmetric Difference ('^'): Returns elements that are in either of the sets, but not in both.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

union_set = set1 | set2  # {1, 2, 3, 4, 5}
intersection_set = set1 & set2  # {3}
difference_set = set1 - set2  # {1, 2}
symmetric_difference_set = set1 ^ set2  # {1, 2, 4, 5}

Modifying Sets:

Sets are mutable, meaning you can add and remove elements:

my_set = {1, 2, 3}

# Adding elements
my_set.add(4)
my_set.update([5, 6])

# Removing elements
my_set.remove(3)
my_set.discard(2)

Membership Test: Check if an element exists in the set.

Length: Get the number of elements in a set.

my_set = {1, 2, 3, 4, 5}

# Membership test
print(3 in my_set)  # True

# Length of the set
print(len(my_set))  # 5

Let's create a simple Python program that demonstrates the use of sets.

PDF Copy Code
def main():
    # Create two sets of numbers
    set1 = {1, 2, 3, 4, 5}
    set2 = {4, 5, 6, 7, 8}

    # Perform set operations
    union_set = set1 | set2
    intersection_set = set1 & set2
    difference_set1 = set1 - set2
    difference_set2 = set2 - set1
    symmetric_difference_set = set1 ^ set2

    # Print the results
    print("Set 1:", set1)
    print("Set 2:", set2)
    print("Union:", union_set)
    print("Intersection:", intersection_set)
    print("Difference (Set 1 - Set 2):", difference_set1)
    print("Difference (Set 2 - Set 1):", difference_set2)
    print("Symmetric Difference:", symmetric_difference_set)

if __name__ == "__main__":
    main()
Output:
Set 1: {1, 2, 3, 4, 5}
Set 2: {4, 5, 6, 7, 8}
Union: {1, 2, 3, 4, 5, 6, 7, 8}
Intersection: {4, 5}
Difference (Set 1 - Set 2): {1, 2, 3}   
Difference (Set 2 - Set 1): {8, 6, 7}   
Symmetric Difference: {1, 2, 3, 6, 7, 8}

This program defines a 'main()' function where we create two sets, 'set1' and 'set2'. We then perform various set operations and print the results. Finally, we call the 'main()' function to execute the program.

Sets are useful when you need to work with unique elements and perform operations like finding intersections or differences between collections.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.