boucle « for »

Python intermédiaire

Hugo Bowne-Anderson

Data Scientist at DataCamp

boucle « for »

for var in seq :
    expression
  • Pour chaque variable dans la séquence, exécutez l'expression
Python intermédiaire

fam

family.py

fam = [1.73, 1.68, 1.71, 1.89]

print(fam)
[1.73, 1.68, 1.71, 1.89]
Python intermédiaire

fam

family.py

fam = [1.73, 1.68, 1.71, 1.89]
print(fam[0])
print(fam[1])
print(fam[2])
print(fam[3])
1.73
1.68
1.71
1.89
Python intermédiaire

boucle « for »

for var in seq :
    expression

family.py

fam = [1.73, 1.68, 1.71, 1.89]
for height in fam : 
    print(height)
Python intermédiaire

boucle « for »

for var in seq :
    expression

family.py

fam = [1.73, 1.68, 1.71, 1.89]
for height in fam : 
    print(height)
    # first iteration
    # height = 1.73
1.73
Python intermédiaire

boucle « for »

for var in seq :
    expression

family.py

fam = [1.73, 1.68, 1.71, 1.89]
for height in fam : 
    print(height)
    # second iteration
    # height = 1.68
1.73
1.68
Python intermédiaire

boucle « for »

for var in seq :
    expression

family.py

fam = [1.73, 1.68, 1.71, 1.89]
for height in fam : 
    print(height)
1.73
1.68
1.71
1.89
  • Aucun accès aux index
Python intermédiaire

boucle « for »

for var in seq :
    expression

family.py

fam = [1.73, 1.68, 1.71, 1.89]
  • ???
index 0: 1.73
index 1: 1.68
index 2: 1.71
index 3: 1.89
Python intermédiaire

enumerate

for var in seq :
    expression

family.py

fam = [1.73, 1.68, 1.71, 1.89]

for index, height in enumerate(fam) : print("index " + str(index) + ": " + str(height))
index 0: 1.73
index 1: 1.68
index 2: 1.71
index 3: 1.89
Python intermédiaire

Boucle sur une chaîne

for var in seq :
    expression

strloop.py

for c in "family" :
    print(c.capitalize())
F
A
M
I
L
Y
Python intermédiaire

Passons à la pratique !

Python intermédiaire

Preparing Video For Download...