Python - Sets

01-Feb-2024

Explore the versatility of sets in Python. Learn how to create, manipulate, and perform set operations, unlocking the power of this unordered and unique collection of elements.

Sets in Python: Unleashing Uniqueness and Set Operations

Sets in Python are unordered collections of unique elements. They offer a versatile way to perform mathematical set operations, such as union, intersection, and difference. Let's explore the characteristics and capabilities of sets:


1. Set Declaration :


   - Sets are declared using curly braces {} or the set() constructor.


  
# Set Declaration Example
my_set = {1, 3, 5} # Creating a set with elements 1, 3, and 5
    


2. Uniqueness :


   - Sets enforce uniqueness, meaning they cannot contain duplicate elements.


  
# Set Uniqueness Example
my_set = {1, 2, 3, 2, 4, 5} # Creating a set with duplicate elements
unique_set = set(my_set) # Creating a new set with unique elements

print(unique_set) # result is {1, 2, 3, 4, 5}
    


3. Set Operations :


   - Sets support various operations such as union (|), intersection (&), difference (-), and symmetric difference (^).


  
# Set Operations Example
set_a = {1, 2, 3, 6} # Creating set A set_b = {3, 4, 5, 6} # Creating set B
# Union - Elements present in either set A or set B or both union_result = set_a | set_b # {1, 2, 3, 4, 5 ,6}
# Intersection - Elements present in both set A and set B intersection_result = set_a & set_b # {3 , 6}
# Difference - Elements present in set A but not in set B difference_result = set_a - set_b # {1, 2}
# Symmetric Difference - Elements present in either set A or set B, but not both symmetric_difference_result = set_a ^ set_b # {1, 2, 4, 5}
    


4. Adding and Removing Elements :


   - Elements can be added to a set using the add() method and removed using remove() or discard().


# Adding and Removing Elements from a Set Example

my_set = {2, 3, 5} # Creating a set
my_set.add(4) # Adding an element to the set
#Output
print(my_set) # {2, 3, 4, 5}

my_set.remove(2) # Removing an element from the set
# Output
print(my_set) # {3, 4, 5}
my_set.discard(5) # Discarding an element from the set # Output: print(my_set) # {3, 4}
    


5. Set Comprehensions :


   - Set comprehensions provide a concise way to create sets based on existing sequences or ranges.


  
# Set Comprehension Example
numbers = {1, 2, 3, 4, 5, 6} # Original set
squared_numbers = {x**2 for x in numbers} # Set comprehension
# Output:
print(squared_numbers) # {1, 4, 9, 16, 25, 36}
    


6. Frozen Sets :

   - Python offers an immutable version of sets called frozen sets, created using the frozenset() constructor.


 
# Frozen Set Example
numbers = {1, 2, 3, 4, 5} # Original set
frozen_numbers = frozenset(numbers) # Creating a frozen set
# Output:
print(frozen_numbers) # frozenset({1, 2, 3, 4, 5})
# Attempting to modify a frozen set will result in an error
frozen_numbers.add(6) # This will raise an AttributeError

     
Understanding sets equips you with a powerful tool for handling collections of unique elements and performing set operations efficiently. Whether it's filtering out duplicates or determining the common elements between two sets, sets provide a concise and effective solution.

Comments