A Set in Python is a collection of unique elements and is a useful data structure for storing and manipulating data. It is an unordered collection of unique elements and they areÂ **mutable**Â andÂ **iterable**. **Mutable**Â means that an object can be changed after it is instantiated.

Note that both list and set are used to store the elements, the difference being; the list can have duplicate values whereas a set will have only unique values. In this article, we will explain the Python set data structure and operations you perform on sets with examples.

## Table of contents

## 1. Definition of Set in Python

Sets are useful for storing a collection of elements when the order of the elements is not important, and you only need to know if an element is or is not in the set.

In Python, a set is a collection of unique elements and it is an unordered data structure, which means that the elements in a set are not stored in a particular order, and they cannot be accessed by their index.

```
# Create a set
permissions = {'read', 'write', 'delete'}
resource = 'read'
if resource in permissions:
print('Access granted')
else:
print('Access denied')
```

## 2. Create Python Sets Object

There are multiple ways you can create a set object in Python. Following section describes some.

### 2.1 Create a Set using Curly Braces

Curly braces (`{}`

) are used in Python to define several types of objects, including sets, dictionaries, and set comprehensions.

In Python, sets can contain elements of any data type, including integers, floating-point numbers, strings, and even other data structures such as lists or dictionaries.

```
# Create set of numbers
numbers = {1,2,3,4,5,6}
# Create a set of strings
permissions = {'read', 'write', 'delete'}
# Create a dictionary
my_dict = {'name': 'John', 'age': 30, 'country': 'USA'}
```

### 2.2 Create using set() Function

Alternatively, you can also create a set by using set() function. This function just takes one argument as input hence you need to pass a sequence type which is a list or range. For example,

```
# Create using set() function
numb = set((1,2,3,4,5))
# Create using set() function
numb = set(range(0,10))
```

### 2.3 Create Set using Generator Expression

You can use a generator expression to create a set in Python by passing the generator expression to the `set`

function. A generator expression is a concise way to create a generator object in Python.

Generators are useful when you want to generate a sequence of elements, but you donâ€™t want to store all the elements in memory at once.

```
# Create a set of even numbers between 0 and 10
even_numbers = set(x for x in range(11) if x % 2 == 0)
print(even_numbers)
# Outputs:
# {0, 2, 4, 6, 8, 10}
```

### 2.4 Create Set using a Comprehension

To create a set using a comprehension in Python, you can use a set comprehension. A set comprehension is defined using curly braces and a single expression that is used to calculate the elements of the set.

```
# Create a set of uppercase vowels
vowels = {c for c in 'aeiouAEIOU' if c.isupper()}
print(vowels)
# Outputs:
# {'A', 'E', 'I', 'O', 'U'}
```

In this example, the set comprehension `{c for c in 'aeiouAEIOU' if c.isupper()}`

generates a sequence of uppercase vowels, and the curly braces define a set containing those elements.

## 3. Python Set Operations

You can perform several operations on a Python set like intersection, union e.t.c. Below are some examples of python set methods. Note that instead of methods you can also use set operators to perform these.

### 3.1 Set Intersection using â€˜&â€™ Operator

The `&`

symbol is the intersection operator in Python. It is used to find the intersection of two sets, which is the set of elements that are common to both sets. You can also use the intersection() method to find the difference.

Hereâ€™s an example of using the `intersection`

method to find the common elements in two sets:

```
# Create two sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# Set intersection using &
intersection = set1 & set2
print(intersection)
# Using intersection()
intersection = set1.intersection(set2)
print(intersection)
# Output:
# {3, 4}
```

### 3.2 Difference Between Two Sets using the â€˜-â€˜ Operator

The `-`

operator is the difference operator in Python, and it is used to find the difference between two sets. The `-`

operator is similar to the `difference()`

method of sets, which also finds the elements in one set that are not in another set.

```
# Create two sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# Set differences
difference = set1 - set2
print(difference)
# Output:
# {1, 2}
```

See the following key points to understand the `-`

and the `difference()`

function:

- The
`-`

operator is used to find the difference between two sets. - The
`difference`

method returns the set of elements that are in the first set but not in any of the other sets. - The
`-`

operator only takes two sets, while the`difference`

method can take multiple sets as arguments.

### 3.3 Check if one set is a superset of another using the â€˜>=â€™ operator

The `>=`

operator can be used to check if one set is a superset of another set. A set is a superset of another set if it contains all the elements of that set.

The `>=`

operator is similar to the `issuperset`

method of sets, which also checks if one set is a superset of another set.

```
# Create two sets
set1 = {1, 2, 3, 4}
set2 = {3, 4}
is_superset = set1 >= set2
print(is_superset)
# Output:
# True
```

### 3.4 Check if one set is a subset of another using the â€˜<=â€™ operator

The `<=`

operator can be used to check if one set is a subset of another set. A set is a subset of another set if it contains only elements that are also in that set.

The `<=`

operator takes two operands (sets), while the `issubset`

method takes one set as an argument. See the following example.

```
set1 = {1, 2, 3, 4}
set2 = {3, 4}
is_subset = set2 <= set1
print(is_subset)
# Output:
# True
```

## Summary and Conclusion

Now you have learned all about Python sets and how to use create and use it. You can create sets, and perform set operations such as intersection, difference, and subset. If you have any questions about Python sets leave a comment and Iâ€™ll be happy to help.