-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyield1.py
More file actions
43 lines (41 loc) · 848 Bytes
/
yield1.py
File metadata and controls
43 lines (41 loc) · 848 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
#ex6-7-1.py
#yield1.py
def my_range(start,stop,step=1):
if start<stop:
i=start
while i<stop:
yield i
i+=step
else:
i=start
while i>stop:
yield i
i+=step
gen1=my_range(1,2,3)
print(gen1)
print(type(gen1))
for i in gen1:
print(i)
gen1=my_range(2,11,4)
print(gen1)
print(type(gen1))
for i in gen1:
print(i)
#next
gen1=my_range(2,11,4)
print("use next to print a generator")
flag=True
while flag:
try:
#next(): return current elements and points the next one
print(next(gen1))
except StopIteration:
print("error")
flag=False
#output will be infite if step==0
#output: 10\n10\n10\n10\n10\n...
gen1=my_range(10,12,0)
print(gen1)
print(type(gen1))
for i in gen1:
print(i)