1 Commits

Author SHA1 Message Date
124bff5e06 Kapitel 18: Generatoren und list comprehension 2023-06-08 19:38:09 +02:00
9 changed files with 62 additions and 0 deletions

View File

@@ -22,4 +22,5 @@ CC-BY-SA Olli Graf
|15 | GPIO|
|16 | Testing|
|17 | Datenbank|
|17 | Generatoren und list comprehension|

2
flask/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
./__pycache__
renderhtml/__pycache__

1
teil18/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
./__pycache__

15
teil18/fib_gen.py Normal file
View File

@@ -0,0 +1,15 @@
import sys
def fib_generator():
a,b = 0,1
while True:
yield a
a,b = b, a+b
fibo = fib_generator()
for _ in range(int(sys.argv[1])):
print(next(fibo))

6
teil18/squares_for.py Normal file
View File

@@ -0,0 +1,6 @@
squares = []
for x in range(1,6):
squares.append(x**2)
print(squares)

View File

@@ -0,0 +1,3 @@
squares = [x **2 for x in range(1,6)]
print(squares)

11
teil18/vornamen_filter.py Normal file
View File

@@ -0,0 +1,11 @@
schueler = [
{'name':'Simpson','vorname': 'Bart'},
{'name':'Simpson','vorname':'Lisa'},
{'name':'van Houten','vorname':'Milhouse'},
{'name':'Wiggum','vorname':'Ralph'},
{'name':'Jones','vorname':'Jimbo'}
]
vornamen = [s['vorname'] for s in schueler if s['name'] == 'Simpson']
print(vornamen)

4
teil18/zahlen_for.py Normal file
View File

@@ -0,0 +1,4 @@
zahlen = [1,2,3,4,5,6,7,8,9,10]
for n in zahlen:
print(n)

19
teil18/zahlen_gen.py Normal file
View File

@@ -0,0 +1,19 @@
def gen_zahl():
n = 0
while n < 10:
n += 1
yield n
zahlen = gen_zahl()
print(next(zahlen))
print(next(zahlen))
print(next(zahlen))
print(next(zahlen))
print(next(zahlen))
print(next(zahlen))
print(next(zahlen))
print(next(zahlen))
print(next(zahlen))
print(next(zahlen))