If you’re new to Python, you’ve probably come across data structures like lists, strings, and tuples in python. But have you ever heard of sets in python? In Python, a set is a collection of unique elements in an unordered fashion. It is defined using curly braces{} or the set constructor. This blog post demonstrates different python set operations.

## What are Sets in Python?

Sets in Python are very much like lists except that their elements are immutable (you cannot change or edit an element of a set after it has been declared). But, you can surely add/delete elements from the set. If it seemed confusing, let me try to summarise:

A set is a mutable, unordered collection of elements, where the elements themselves are immutable. Another feature of a set is that it can contain elements of various types. This implies that you can have a collection of numbers, strings, and even tuples in the same set!

## Python Set Operations

If you remember your high school basic math, you’ll probably recall mathematical set operations like union, intersection, difference, and symmetric difference. Now, the interesting part is we can do the same thing with Python sets.

### Set Union

The union of two sets is the set that contains all of the elements from both sets without duplicates. To find the union of two Python sets, use the union() method or the | syntax.

Here is an example of how to perform a union set operation in python.

set1 = {11, 22, 33} set2 = {33, 54, 75} # Perform union operation using the `|` operator or the `union()` method union_set = set1 | set2 # Or: union_set = set1.union(set2) print(union_set)

**Output:**

`{33, 22, 54, 75, 11}`

**Explanation:** In this example, we have two sets set1 and set2. We can perform a union operation using either the | operator or the union() method, which combines all the elements from both sets and removes any duplicates. The resulting union_set contains all elements from set1 and set2.

### Set Intersection

The intersection of two sets is the set that contains all of the common elements of both sets. To find the intersection of two Python sets, we can either use the intersection() method or & operator.

Here is an example to demonstrate this.

set1 = {11, 22, 33} set2 = {33, 54, 75} # Perform intersection operation using the `&` operator or the `intersection()` method intersection_set = set1 & set2 # Or: intersection_set = set1.intersection(set2) print(intersection_set)

**Output:**

`{33}`

**Explanation:** In this example, we have two sets set1 and set2. We can perform an intersection operation using either the & operator or the intersection() method, which returns a new set that contains only the elements that are common to both set1 and set2. The resulting intersection_set contains only element 33, which is present in both set1 and set2.

### Set Difference

The difference between the two sets is the set of all the elements in the first set that are not present in the second set. To accomplish this, we can either use the difference() function or the – operator in python.

Here is an example to demonstrate this.

set1 = {1, 2, 3, 4, 5} set2 = {3, 4, 5, 6, 7} # Using the difference() method set3 = set1.difference(set2) print(set3) # Using the - operator set4 = set1 - set2 print(set4)

**Output:**

```
{1, 2}
{1, 2}
```

**Explanation:** In this example, we have two sets set1 and set2 with some common elements. We want to get the elements that are present in set1 but not in set2. We can achieve this by using the difference() method or the – operator.

In the first approach, we use the difference() method of set1 and pass set2 as an argument. This returns a new set set3 that contains the elements that are present in set1 but not in set2.

In the second approach, we use the – operator to perform the set difference operation. This is equivalent to using the difference() method. The result is stored in the set4.

### Set Symmetric Difference

The symmetric difference between the two sets is the set containing all the elements that are either in the first or second set but not in both. In Python, you can use either the symmetric difference() function or the ^ operator to achieve this.

Here is an example that demonstrates this.

set1 = {1, 2, 3, 4, 5} set2 = {3, 4, 5, 6, 7} # Using the symmetric_difference() method set3 = set1.symmetric_difference(set2) print(set3) # Using the ^ operator set4 = set1 ^ set2 print(set4)

**Output:**

```
{1, 2, 6, 7}
{1, 2, 6, 7}
```

**Explanation:** In this example, we have two sets set1 and set2 with some common elements. We want to get the elements that are present in either of the sets, but not in both. We can achieve this by using the symmetric_difference() method or the ^ operator.

In the first approach, we use the symmetric_difference() method of set1 and pass set2 as an argument. This returns a new set set3 that contains the elements that are present in either of the sets, but not in both.

In the second approach, we use the ^ operator to perform the set symmetric difference operation. This is equivalent to using the symmetric_difference() method. The result is stored in the set4.

## Other Set Operations in Python

These are not so common but they are useful in seeing how sets are related to each other.

### Issubset

The issubset() operation is used in Python to check whether a set is a subset of another set.

Here’s an example to demonstrate this:

set1 = {1, 2, 3, 4, 5} set2 = {2, 4} # Check if set2 is a subset of set1 print(set2.issubset(set1)) # Check if set1 is a subset of set2 print(set1.issubset(set2))

**Output:**

```
True
False
```

**Explanation:** In this example, we have two sets set1 and set2. We want to check if set2 is a subset of set1. We can do this by calling the issubset() method of set2 and passing set1 as an argument. This returns a boolean value True, which indicates that set2 is a subset of set1.

Next, we check if set1 is a subset of set2 by calling the issubset() method of set1 and passing set2 as an argument. This returns a boolean value False, which indicates that set1 is not a subset of set2.

### Issuperset

The issuperset() operation in Python is used to check whether a set is a superset of another set. A superset is a set that contains all the elements of another set.

Here’s an example to demonstrate this:

set1 = {1, 2, 3, 4, 5} set2 = {2, 4} # Check if set1 is a superset of set2 print(set1.issuperset(set2)) # Check if set2 is a superset of set1 print(set2.issuperset(set1))

**Output:**

```
True
False
```

**Explanation:** In this example, we have two sets set1 and set2. We want to check if set1 is a superset of set2. We can do this by calling the issuperset() method of set1 and passing set2 as an argument. This returns a boolean value True, which indicates that set1 is a superset of set2.

Next, we check if set2 is a superset of set1 by calling the issuperset() method of set2 and passing set1 as an argument. This returns a boolean value False, which indicates that set2 is not a superset of set1.

### Isdisjoint

The isdisjoint() operation in Python is used to check whether two sets have any common elements. If the two sets have no common elements, then they are said to be disjoint.

Here’s an example to demonstrate this:

set1 = {1, 2, 3, 4, 5} set2 = {6, 7, 8} # Check if set1 and set2 are disjoint print(set1.isdisjoint(set2)) set3 = {4, 5, 6} set4 = {6, 7, 8} # Check if set3 and set4 are disjoint print(set3.isdisjoint(set4))

**Output:**

```
True
False
```

**Explanation:** The isdisjoint() method in Python is used to check whether two sets have any common elements. If the two sets have no common elements, then they are said to be disjoint.

Here’s an example to illustrate the isdisjoint() method:

In this example, we have two sets set1 and set2. We want to check if they are disjoint, i.e., they have no common elements. We can do this by calling the isdisjoint() method of set1 and passing set2 as an argument. This returns a boolean value True, which indicates that set1 and set2 are disjoint.

Next, we have two sets set3 and set4. We want to check if they are disjoint or not. We can do this by calling the isdisjoint() method of set3 and passing set4 as an argument. This returns a boolean value False, which indicates that set3 and set4 are not disjoint, since they have a common element 6.

**Conclusion**

In conclusion, Python set operations provide a powerful and flexible way to work with sets. In this article, we have covered various set operations such as union, intersection, difference, symmetric difference, subset, superset, and disjoint, along with examples to demonstrate each operation. Understanding these set operations can help simplify complex programming problems and make code more efficient.

## FAQs

Here are some frequently asked questions on python set operations.

**Q1: Can a set have duplicate elements?**

**A:** No, a set cannot have duplicate elements. If you try to add a duplicate element to a set, it will simply be ignored.

**Q2: What is the difference operation on sets?**

**A:** The difference operation on sets is used to get the elements that are present in one set but not in the other.

**Q3: What is the symmetric difference operation on sets?**

**A:** The symmetric difference operation on sets is used to get the elements that are present in either of the sets, but not in both.

**Q4: Are sets mutable in Python?**

**A:** Yes, sets are mutable in Python, which means that the elements can be added or removed from the set.

**Q5: Can sets contain duplicate elements?**

**A:** No, sets cannot contain duplicate elements. If you try to add a duplicate element to a set, it will be ignored.

**Q6: What is the difference between a subset and a proper subset?**

**A:** A subset is a set that contains all the elements of another set (and possibly some additional elements), while a proper subset is a subset that contains all the elements of another set, but no additional elements. In other words, a proper subset is a subset that is not equal to the original set.