# Set (ensemble)



Un set est une collection **non ordonnée** et **non indexée**. On ne peut pas modifier un item, mais on peut en ajouter.



##### Créer un set:

```python
>>> un_set = {"alpha", "beta", "gamma"}
>>> un_set
{'alpha', 'beta', 'gamma'}
>>> type(un_set)
<class 'set'>
```

```python
>>> autre_set = {"alpha", "beta", "gamma", "beta", "alpha"}
>>> len(autre_set)
3
>>> autre_set
{'beta', 'alpha', 'gamma'}

```

```python
# Constructor set()
>>> un_set = set(("alpha", "beta", "gamma"))
>>> un_set
{'gamma', 'alpha', 'beta'}
```

##### Ajouter un item:

```python
>>> un_set = {"alpha", "beta", "gamma"}
>>> un_set.add("delta")
>>> un_set
{'gamma', 'alpha', 'beta', 'delta'}
```

##### Ajouter plusieurs items:

```python
>>> un_set = {"alpha", "beta", "gamma"}
>>> un_set.update(["delta","zeta","epsilon"])
>>> un_set
{'zeta', 'alpha', 'beta', 'delta', 'gamma', 'epsilon'}
```

##### Longueur d'un set:

```python
>>> un_set = {"alpha", "beta", "gamma"}
>>> print(len(un_set))
3
```

##### Supprimer un item (remove):

```python
>>> un_set = {"alpha", "beta", "gamma"}
>>> un_set.remove("beta")
>>> un_set
{'gamma', 'alpha'}

# Si on retire un élément qui n'existe pas => Erreur
>>> un_set.remove("delta")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'delta'
```

##### Supprimer un item (discard):

```python
>>> un_set = {"alpha", "beta", "gamma"}
>>> un_set.discard("beta")
>>> un_set
{'gamma', 'alpha'}

# Si on retire un élément qui n'existe pas.
>>> un_set = {"alpha", "beta", "gamma"}
>>> un_set.discard("delta")
>>> un_set
{'alpha', 'gamma', 'beta'}
```

##### Vider un set:

```python
>>> un_set = {"alpha", "beta", "gamma"}
>>> un_set.clear()
>>> un_set
set()
```

##### Supprimer un set:

```python
>>> del un_set
>>> un_set
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'un_set' is not defined
```

##### Boucle:

```python
un_set = {"alpha", "beta", "gamma"}
for x in un_set:
    print(x)

alpha
beta
gamma
```

##### Test si un item est présent dans le set:

```python
un_set = {"alpha", "beta", "gamma"}
print("alpha" in un_set)

True
```

##### Set => pas d'indice:

```python
>>> un_set = {"alpha", "beta", "gamma"}
>>> un_set[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
```

##### Opérateurs:

- `a | b` (union),
- `a & b` (intersection),
- `a - b` (différence),
- `a ^ b` (différence symétrique)



#### Méthodes:

| Méthodes                      | Description |
| ----------------------------- | ----------- |
| add()                         |             |
| clear()                       |             |
| copy()                        |             |
| difference()                  |             |
| difference_update()           |             |
| discard()                     |             |
| intersection()                |             |
| intersection_update()         |             |
| isdisjoint()                  |             |
| issubset()                    |             |
| issuperset()                  |             |
| pop()                         |             |
| remove()                      |             |
| symmetric_difference()        |             |
| symmetric_difference_update() |             |
| union()                       |             |
| update()                      |             |

