-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerators.py
More file actions
52 lines (46 loc) · 841 Bytes
/
generators.py
File metadata and controls
52 lines (46 loc) · 841 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
47
48
49
50
51
52
def foo():
print ("begin")
for i in range(3):
print ("before yield", i)
yield i
print ("after yield", i)
print ("end")
f = foo()
print(next(f))
print("-------")
print(next(f))
print(next(f))
def city_generator():
yield("Konstanz")
yield("Zurich")
yield("Schaffhausen")
yield("Stuttgart")
x = city_generator()
print(next(x))
def my_gen():
n = 1
print('This is printed first')
# Generator function contains yield statements
#print(n)
yield n
n += 1
print('This is printed second')
#print(n)
yield n
n += 1
print('This is printed at last')
#print(n)
yield n
a=my_gen()
print(next(a))
print(next(a))
print(next(a))
#
for items in a:
print(items)
#
#
a = (x*x for x in range(5))
for it in a:
print(it)
print(sum(a))