# Master sets in python


![Capture rtr.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1660763882506/ZoTkW1-nh.PNG align="left")
In the French school programs of the high school, it is a question of sets from the Second class, and it is a question of Python. We are used to representing sets by lists or dictionaries, but there is another type that can be useful: the set type. Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection that is unordered, unchangeable*, and unindexed.

## Sets in Python: type “set”

This type of object is a bit special compared to lists and dictionaries: it is a mixture of the two from a syntactical point of view.

A set can be declared as follows:

> E = {'1','2','3'}

or

> E = ('123')

These two ways of declaring E give the same result:

```
>>> E
{'1','2','3'}
```

Notice then that the elements of the set are of type string. To have a set of numbers, it will be necessary to use the first syntax:

> E = {1,2,3}
```
>>> E
{1, 2, 3}
```

## Important methods associated with the “set” type

We will start from an empty set E:

> E = set()

## Sets in Python: adding an element: add()

> E.add(1)

Here, we add the number 1 to the set. So we now have:
```
>>> E
{1}
```
## Clear the set: clear()

> E.clear()

At this point, there is nothing left in E: we are back to the empty set.

For the purposes of the sequel, we will construct the set of even numbers from 0 to 20:

> E = set( 2*k for k in range(11) )
```
>>> E
{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
```
## Sets in Python: remove the first element: pop()

> E.pop()

```
>>> E
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
```
## Union of two sets: union()

Also, for the purposes of this paragraph, I will first construct the set of multiples of 3 from 0 to 20:

> F = set( 3*k for k in range(7) )
```
>>> F
{0, 3, 6, 9, 12, 15, 18}
```

## In addition, to obtain the union of the sets E and F, we write:

> U = E.union(F)

```
>>> U
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
```
## Intersection of two sets: intersection()
> R = E.intersection(F)
```
>>> R
{18, 12, 6}
```
## Delete an element: discard()

> E.discard(10)
```
>>> E
{2, 4, 6, 8, 12, 14, 16, 18, 20}
```
## Difference between two sets: difference()
> D = E.difference(F)  # ensemble E auquel on a enlevé tous les éléments de F
> DD = F.difference(E) # ensemble F auquel on a enlevé tous les éléments de E
```
>>> D 
{2, 4, 8, 14, 16, 20}
>>> DD # ensemble F auquel on a enlevé tous les éléments de E
{0, 9, 3, 15}
```
## The symmetric difference (exclusion join): symmetric_difference()

> S = E.symmetric_difference(F)
```
>>> S
{0, 2, 3, 4, 8, 9, 14, 15, 16, 20}
```

## The exclusion join of E and F is the set of elements that are in E or in F, but not in their intersection.

You can also use the following syntax:

```
>>> S = E ^ F
```
## Sets in Python: Adding Multiple Items: update()
> X = {1,2,3,4,5}
> Y = {50,60,70,80}
> X.update(Y)
```
>>> X
{1, 2, 3, 4, 5, 70, 80, 50, 60}
```
### exercise
> Is it a subset? issubset()
```
>>> F.issubset(E)
False
```
> Indeed, F is not a subset of E.

> Are the two sets disjoint?
```
>>> E.isdisjoint(F)
False
```
Indeed, our sets E and F are not disjoint here because they have elements in common.


