Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

Python Sets

A set is an unordered collection of unique elements in Python. Sets can be used to perform various operations like membership testing, intersection, union, and difference. This tutorial covers the basics of sets, creating sets, and common set operations.

  • Creating sets

Sets can be created in two ways:

  • Using curly braces {} and separating elements with commas:
my_set = {1, 2, 3, 4, 5}
print(my_set)  # Output: {1, 2, 3, 4, 5}
  • Using the set() constructor and passing an iterable (e.g., a list or tuple) as an argument:
my_list = [1, 2, 3, 4, 5]
my_set = set(my_list)
print(my_set)  # Output: {1, 2, 3, 4, 5}

Note that sets do not allow duplicate elements, so any duplicates in the input will be removed:

my_set = {1, 2, 3, 4, 5, 5, 4, 3}
print(my_set)  # Output: {1, 2, 3, 4, 5}
  • Adding elements to a set

You can use the add() method to add a single element to a set:

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

To add multiple elements, use the update() method and pass an iterable as an argument:

my_set = {1, 2, 3}
my_set.update([4, 5, 6])
print(my_set)  # Output: {1, 2, 3, 4, 5, 6}
  • Removing elements from a set

You can use the remove() method to remove an element from a set. However, it will raise a KeyError if the element is not found. Alternatively, you can use the discard() method, which does not raise an error if the element is not found:

my_set = {1, 2, 3, 4, 5}
my_set.remove(5)
my_set.discard(6)
print(my_set)  # Output: {1, 2, 3, 4}
  • Common set operations
  • Union: The union of two sets is a new set containing all elements from both sets. Use the union() method or the | operator:
set_a = {1, 2, 3}
set_b = {3, 4, 5}
union_set = set_a.union(set_b)
# Alternatively: 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 the elements present in both sets. Use the intersection() method or the & operator:
set_a = {1, 2, 3}
set_b = {3, 4, 5}
intersection_set = set_a.intersection(set_b)
# Alternatively: intersection_set = set_a & set_b
print(intersection_set)  # Output: {3}
  1. Creating and initializing sets in Python:

    • Description: Sets in Python are unordered collections of unique elements. They can be created using curly braces {} or the set() constructor.
    • Example Code:
      # Using curly braces
      my_set = {1, 2, 3}
      
      # Using set() constructor
      another_set = set([3, 4, 5])
      
  2. Common set operations in Python:

    • Description: Sets support various operations, including union, intersection, difference, and more.
    • Example Code:
      set1 = {1, 2, 3}
      set2 = {3, 4, 5}
      
      # Union
      union_set = set1.union(set2)
      
      # Intersection
      intersection_set = set1.intersection(set2)
      
      # Difference
      difference_set = set1.difference(set2)
      
  3. Adding and removing elements from sets in Python:

    • Description: Elements can be added to a set using add() and removed using remove() or discard().
    • Example Code:
      my_set = {1, 2, 3}
      
      # Adding elements
      my_set.add(4)
      
      # Removing elements
      my_set.remove(2)
      
  4. Set union, intersection, and difference in Python:

    • Description: Set operations like union, intersection, and difference can be performed using methods or operators (|, &, -).
    • Example Code:
      set1 = {1, 2, 3}
      set2 = {3, 4, 5}
      
      # Union
      union_set = set1 | set2
      
      # Intersection
      intersection_set = set1 & set2
      
      # Difference
      difference_set = set1 - set2
      
  5. Checking membership in sets in Python:

    • Description: Use in to check if an element is present in a set.
    • Example Code:
      my_set = {1, 2, 3}
      
      # Membership check
      is_present = 2 in my_set
      
  6. Working with frozen sets in Python:

    • Description: Frozen sets are immutable sets. Once created, elements cannot be added or removed.
    • Example Code:
      my_frozen_set = frozenset([1, 2, 3])
      
  7. Set comprehension in Python:

    • Description: Set comprehensions provide a concise way to create sets using a single line of code.
    • Example Code:
      squares_set = {x**2 for x in range(1, 6)}
      
  8. Sorting and converting sets in Python:

    • Description: Sets are unordered, but you can convert them to sorted lists or perform sorting within set comprehensions.
    • Example Code:
      my_set = {3, 1, 4, 1, 5, 9}
      
      # Convert to sorted list
      sorted_list = sorted(my_set)
      
      # Set comprehension with sorting
      squares_set = {x**2 for x in sorted(my_set)}