Python Set 


In this tutorial, we will explore the concept of sets in Python.

A set is an unordered collection of unique elements.

It is an essential data structure for handling collections of distinct items.

We will cover various operations and methods available for working with sets in Python.

 

Creating a Set


To create a set in Python, you can use curly braces ({}) or the ‘set()’ constructor.

Here are a few examples:

# Empty set
my_set1 = set()

# Set with elements
my_set2 = {1, 2, 3}

# Using the set() constructor
my_set3 = set([4, 5, 6])


In the above examples, my_set2 and my_set3 are sets with the values 1, 2, and 3, and 4, 5, and 6, respectively.

 

Adding and Removing Elements


You can add elements to a set using the ‘add()’ method or the ‘update()’ method. The ‘add()’ method adds a single element to the set, while the ‘update()’ method can add multiple elements.

Here's an example:

my_set = {1, 2, 3}
my_set.add(4)
my_set.update([5, 6, 7])
print(my_set)  # Output: {1, 2, 3, 4, 5, 6, 7}


To remove elements from a set, you can use the ‘remove()’ method or the ‘discard()’ method. The ‘remove()’ method removes a specific element from the set, while the ‘discard()’ method removes an element if it exists in the set, but does not raise an error if the element is not found.

Here's an example:

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

 

Set Operations


Python sets support various set operations such as union, intersection, difference, and symmetric difference. These operations can be performed using built-in operators or methods.

Here are some examples:

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

# Union
union_set = set1 | set2
print(union_set)  # Output: {1, 2, 3, 4}

# Intersection
intersection_set = set1 & set2
print(intersection_set)  # Output: {2, 3}

# Difference
difference_set = set1 - set2
print(difference_set)  # Output: {1}

# Symmetric Difference
symmetric_difference_set = set1 ^ set2
print(symmetric_difference_set)  # Output: {1, 4}


You can also use corresponding set methods ( ‘union()’, ‘intersection()’, ‘difference()’, ‘symmetric_difference()’) to perform these operations.

 

Set Methods


Python provides several built-in methods for working with sets. Here are some commonly used set methods:

set.add(element): Adds an element to the set.
set.remove(element): Removes an element from the set. Raises a KeyError if the element is not found.
set.discard(element): Removes an element from the set if it exists, but does not raise an error if the element is not found.
set.clear(): Removes all elements from the set.
set.copy(): Returns a shallow copy of the set.
set.update(other_set): Updates the set by adding elements from another set.
set.union(other_set): Returns a new set that contains all unique elements from the set and another set.
set.intersection(other_set): Returns a new set that contains common elements between the set and another set.
set.difference(other_set): Returns a new set that contains elements present in the set but not in another set.
set.symmetric_difference(other_set): Returns a new set that contains elements that are present in either the set or another set, but not in both.


Here's an example that demonstrates the usage of some of these methods:

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

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

set1.remove(2)
print(set1)  # Output: {1, 3, 4}

set1.update(set2)
print(set1)  # Output: {1, 3, 4, 2}

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

set1.clear()
print(set1)  # Output: set()


These are just a few examples of the many set methods available in Python. You can explore more set methods in the Python documentation.

 

Frozen Sets


In addition to mutable sets, Python also provides immutable sets called frozen sets. Frozen sets are created using the frozenset() constructor and are useful when you need a set that cannot be modified. Since frozen sets are immutable, you cannot add or remove elements from them. However, you can perform set operations on frozen sets. Here's an example:

frozen_set = frozenset([1, 2, 3])
print(frozen_set)  # Output: frozenset({1, 2, 3})


Summary


In this tutorial, we covered the basics of working with sets in Python. We learned how to create sets, add and remove elements, perform set operations, use set methods, and work with frozen sets. Sets are valuable data structures for handling collections of unique items. With the knowledge gained from this tutorial, you should be able to effectively use sets in your Python programs for various tasks.
 

© 2022-2023 All rights reserved.