Kapitel 18: Generatoren und list comprehension
This commit is contained in:
1
teil18/.gitignore
vendored
Normal file
1
teil18/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
./__pycache__
|
15
teil18/fib_gen.py
Normal file
15
teil18/fib_gen.py
Normal 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
6
teil18/squares_for.py
Normal file
@@ -0,0 +1,6 @@
|
||||
squares = []
|
||||
|
||||
for x in range(1,6):
|
||||
squares.append(x**2)
|
||||
|
||||
print(squares)
|
3
teil18/squares_list_comprehension.py
Normal file
3
teil18/squares_list_comprehension.py
Normal file
@@ -0,0 +1,3 @@
|
||||
squares = [x **2 for x in range(1,6)]
|
||||
|
||||
print(squares)
|
11
teil18/vornamen_filter.py
Normal file
11
teil18/vornamen_filter.py
Normal 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
4
teil18/zahlen_for.py
Normal 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
19
teil18/zahlen_gen.py
Normal 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))
|
Reference in New Issue
Block a user