for-Schleife

Python für Fortgeschrittene

Hugo Bowne-Anderson

Data Scientist at DataCamp

for-Schleife

for var in seq :
    expression
  • „Führe für jede Variable in einer Sequenz folgende Anweisungen aus.“
Python für Fortgeschrittene

fam

family.py

fam = [1.73, 1.68, 1.71, 1.89]

print(fam)
[1.73, 1.68, 1.71, 1.89]
Python für Fortgeschrittene

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 für Fortgeschrittene

for-Schleife

for var in seq :
    expression

family.py

fam = [1.73, 1.68, 1.71, 1.89]
for height in fam : 
    print(height)
Python für Fortgeschrittene

for-Schleife

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 für Fortgeschrittene

for-Schleife

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 für Fortgeschrittene

for-Schleife

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
  • Kein Zugriff auf Indizes
Python für Fortgeschrittene

for-Schleife

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 für Fortgeschrittene

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 für Fortgeschrittene

Iterieren über Zeichenfolgen

for var in seq :
    expression

strloop.py

for c in "family" :
    print(c.capitalize())
F
A
M
I
L
Y
Python für Fortgeschrittene

Lass uns üben!

Python für Fortgeschrittene

Preparing Video For Download...