1. Introduction

In Python, a set is an unordered collection of unique elements. This means that each element in a set must be unique, and there can be no duplicates. Sets are defined using curly braces {}. For example, the following code creates a set of numbers:

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

Notice that the numbers in the set are separated by commas, and the set is enclosed in curly braces. Sets can also be created using the set() constructor. For example:  

numbers = set([1, 2, 3, 4, 5])

In this example, we are creating a set from a list. The set() constructor takes an iterable object, such as a list or a tuple, and returns a new set containing the unique elements of the iterable.  

2. Operations on Sets

Python sets support several operations, including union, intersection, difference, and symmetric difference. These operations are performed using operators or methods.

2.1. Union

The union of two sets contains all the elements from both sets. The union is denoted by the symbol | or by using the union() method. For example:

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
union_set = set1 | set2
print(union_set)

Output:

{1, 2, 3, 4, 5, 6}

In this example, we are creating two sets set1 and set2, and then finding their union using the | operator.

2.2. Intersection

The intersection of two sets contains only the elements that are common to both sets. The intersection is denoted by the symbol & or by using the intersection() method. For example:

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
intersection_set = set1 & set2
print(intersection_set)

Output:

{3, 4}

In this example, we are creating two sets set1 and set2, and then finding their intersection using the & operator.

2.3. Difference

The difference of the two sets contains the elements that are in one set but not in the other set. The difference is denoted by the symbol - or by using the difference() method. For example:

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
difference_set = set1 - set2
print(difference_set)

Output:

{1, 2}

In this example, we are creating two sets set1 and set2, and then finding their difference using the - operator.

2.4. Symmetric Difference

The symmetric difference of the two sets contains the elements that are in either of the sets, but not in both. The symmetric difference is denoted by the symbol ^ or by using the symmetric_difference() method. For example:

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set)

Output:

{1, 2, 5, 6}

In this example, we are creating two sets set1 and set2, and then finding their symmetric difference using the symmetric_difference() method.

3. Set Methods

In addition to the operations discussed above, Python sets also support several methods. Let's discuss some of the most commonly used set methods.

3.1. add()

The add() method adds an element to a set. If the element already exists in the set, the set remains unchanged. For example:

fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)

Output:

{"apple", "banana", "cherry", "orange"}

In this example, we are adding the element "orange" to the set fruits.

3.2. remove()

The remove() method removes a specified element from a set. If the element does not exist in the set, the method raises a KeyError exception. For example:

fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits)

Output:

{"apple", "cherry"}

In this example, we are removing the element "banana" from the set fruits.

3.3. discard()

The discard() method removes a specified element from a set. If the element does not exist in the set, the set remains unchanged. For example:

fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits)

Output:

{"apple", "cherry"}

In this example, we are removing the element "banana" from the set fruits using the discard() method.

3.4. pop()

The pop() method removes and returns an arbitrary element from the set. If the set is empty, the method raises a KeyError exception. For example:

fruits = {"apple", "banana", "cherry"}
x = fruits.pop()
print(x)
print(fruits)

Output:

"apple"
{"banana", "cherry"}

In this example, we are removing an arbitrary element from the set fruits using the pop() method.

3.5. clear()

The clear() method removes all elements from a set. For example:

fruits = {"apple", "banana", "cherry"}
fruits.clear()
print(fruits)

Output:

set()

In this example, we are removing all elements from the set fruits using the clear() method.

4. Conclusion

In this blog post, we have discussed sets in Python. We have learned what sets are, how to create sets, and how to perform operations on sets. We have also discussed some of the most commonly used set methods. Sets are a powerful data structure in Python that can help you solve many problems.

Also read:

  1. Set comprehension in Python
  2. Lists in Python
  3. Dictionaries in Python