-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfor.py
More file actions
executable file
·46 lines (34 loc) · 971 Bytes
/
for.py
File metadata and controls
executable file
·46 lines (34 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
# Schleifen:
# 2. Die for-Schleife:
# Die for-Schleife durchläuft ein iterierbares
# Objekt. Alle oben genannten Listen sind solche
# iterierbaren Objekte.
String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for char in String: # type: str
print(char)
# Beim Durchlaufen eines Dictionary wird jedoch nur
# der key zurückgegeben:
Dictionary = {"one": "Eins", "two": "Zwei", "three": "drei"}
for key in Dictionary:
print(key)
print(Dictionary[key])
# besser:
for key, value in Dictionary.items():
print(key, value)
# Ebenso kann ein Tupel oder eine Liste durchlaufen werden
# Mit dem Befehl range() erschafft man ein iterierbares
# Objekt, das mit Zahlen gefüllt ist:
R = range(10)
print(list(R))
# Dabei kann man auch den Startwert und die Schrittweite
# angeben:
R = range(0, 101, 2)
print(list(R))
# Somit kann man eine Zählschleife implementieren:
R = range(10)
for i in R:
print(i)
# oder:
for i in range(10):
print(i)