66 lines
982 B
Markdown
66 lines
982 B
Markdown
# For
|
|
|
|
|
|
|
|
##### Range:
|
|
|
|
```python
|
|
>>> somme = 0
|
|
>>> for i in range(10):
|
|
... somme += i
|
|
...
|
|
>>> print(somme)
|
|
45
|
|
```
|
|
|
|
##### Parcourir une liste:
|
|
|
|
```python
|
|
>>> thislist = ["alpha", "beta", "gamma"]
|
|
>>> for elem in thislist:
|
|
... print(elem)
|
|
...
|
|
alpha
|
|
beta
|
|
gamma
|
|
```
|
|
|
|
##### Parcourir une chaine:
|
|
|
|
```python
|
|
>>> language = "Python"
|
|
>>> for c in language:
|
|
... print("Caractère : ", c)
|
|
...
|
|
Caractère : P
|
|
Caractère : y
|
|
Caractère : t
|
|
Caractère : h
|
|
Caractère : o
|
|
Caractère : n
|
|
```
|
|
|
|
##### Parcourir une chaine par les indices:
|
|
|
|
```python
|
|
>>> language = "Python"
|
|
>>> for i in range(len(language)):
|
|
... print("Caractère d'indice",i," :",language[i])
|
|
...
|
|
Caractère d'indice 0 : P
|
|
Caractère d'indice 1 : y
|
|
Caractère d'indice 2 : t
|
|
Caractère d'indice 3 : h
|
|
Caractère d'indice 4 : o
|
|
Caractère d'indice 5 : n
|
|
```
|
|
|
|
##### Plusieurs variables dans une boucle for:
|
|
|
|
```python
|
|
entrees = [(1, 2), (3, 4), (5, 6)]
|
|
for a, b in entrees:
|
|
print(f"a={a} b={b}")
|
|
```
|
|
|