Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python set

In this tutorial, we'll cover the basics of Python sets and their operations. Sets are unordered collections of unique elements. They are useful for membership testing, removing duplicates from a sequence, and performing mathematical set operations like union, intersection, and difference.

1. Creating a Set

You can create a set by using the set() constructor or by using curly braces {}.

# Using the set constructor
my_set = set([1, 2, 3, 4, 5])

# Using curly braces
my_set = {1, 2, 3, 4, 5}

Note: An empty set can only be created using the set() constructor, as using {} will create an empty dictionary instead.

empty_set = set()

2. Adding Elements to a Set

You can add elements to a set using the add() method.

my_set = {1, 2, 3}
my_set.add(4)

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

3. Removing Elements from a Set

You can remove elements from a set using the remove() or discard() methods. The remove() method raises a KeyError if the element is not found, while discard() does not.

my_set = {1, 2, 3, 4}

my_set.remove(3)  # Removes 3 from the set
my_set.discard(5)  # Does nothing as 5 is not in the set

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

4. Set Operations

Python sets support various operations, such as union, intersection, difference, and symmetric difference.

  • Union: The union of two sets is a new set containing elements present in either of the sets.
set_a = {1, 2, 3}
set_b = {3, 4, 5}

union_set = set_a | set_b
print(union_set)  # Output: {1, 2, 3, 4, 5}
  • Intersection: The intersection of two sets is a new set containing elements present in both sets.
intersection_set = set_a & set_b
print(intersection_set)  # Output: {3}
  • Difference: The difference of two sets is a new set containing elements present in the first set but not in the second set.
difference_set = set_a - set_b
print(difference_set)  # Output: {1, 2}
  • Symmetric Difference: The symmetric difference of two sets is a new set containing elements present in either set, but not in both.
symmetric_difference_set = set_a ^ set_b
print(symmetric_difference_set)  # Output: {1, 2, 4, 5}

5. Set Membership

You can check if an element is a member of a set using the in keyword.

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

print(1 in my_set)  # Output: True
print(6 in my_set)  # Output: False

6. Set Length

You can find the number of elements in a set using the len() function.

my_set = {1, 2, 3, 4, 5}
print(len(my_set))  # Output: 5
  1. How to Create a Set in Python:

    • Use curly braces {} or the set() constructor to create a set.
    # Example
    my_set = {1, 2, 3}
    
  2. Set Data Type in Python:

    • A set is an unordered collection of unique elements.
    # Example
    my_set = {1, 2, 3}
    
  3. Python Set Methods and Operations:

    • Sets have various methods and operations for manipulation, such as add(), remove(), union(), intersection(), etc.
    # Example
    my_set.add(4)          # Add an element
    my_set.remove(2)       # Remove an element
    union_set = set1.union(set2)       # Union of two sets
    intersection_set = set1.intersection(set2)  # Intersection of two sets
    
  4. Accessing and Modifying Set Elements in Python:

    • Sets are mutable, but you can't access elements by index. Use methods like add() and remove() to modify.
    # Example
    my_set.add(4)      # Add an element
    my_set.remove(2)   # Remove an element
    
  5. Iterating Through a Set in Python:

    • Use a loop to iterate through the elements of a set.
    # Example
    for element in my_set:
        print(element)
    
  6. Set Operations in Python (Union, Intersection, etc.):

    • Sets support operations like union, intersection, difference, and symmetric difference.
    # Example
    set1 = {1, 2, 3}
    set2 = {2, 3, 4}
    union_set = set1.union(set2)
    intersection_set = set1.intersection(set2)
    
  7. Set Comprehension in Python:

    • Use set comprehension for concise creation of sets.
    # Example
    squares = {x**2 for x in range(5)}
    
  8. Converting List to Set in Python:

    • Use the set() constructor or {} to convert a list to a set.
    # Example
    my_list = [1, 2, 3, 2, 1]
    my_set = set(my_list)  # Removes duplicates
    
  9. Common Operations on Python Sets:

    • Common operations include add(), remove(), union(), intersection(), difference(), and more.
    # Example
    my_set.add(4)          # Add an element
    my_set.remove(2)       # Remove an element
    union_set = set1.union(set2)       # Union of two sets
    intersection_set = set1.intersection(set2)  # Intersection of two sets