Update
This commit is contained in:
65
docs/Programmation/Python/for.md
Normal file
65
docs/Programmation/Python/for.md
Normal file
@@ -0,0 +1,65 @@
|
||||
# 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}")
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user