-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_generators.py
More file actions
75 lines (51 loc) · 1.05 KB
/
Copy path14_generators.py
File metadata and controls
75 lines (51 loc) · 1.05 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
def my_generator():
for i in range(11):
yield i
g = my_generator()
print(g)
# value = next(g)
# print(value)
print(sum(sorted(g)))
def count_down(start):
print('Starting')
while start > 0:
yield start
start -= 1
cd = count_down(10)
value = next(cd)
print(value)
for i in cd:
print(i)
print('\n')
print('Large data generation')
def first_n(n):
nums = []
index = 1
while index <= n:
nums.append(index)
index += 1
return nums
def first_n_generator(n):
index = 1
while index <= n:
yield index
index += 1
list = first_n(1000000)
print(sum(list))
print(sys.getsizeof(list))
generator = first_n_generator(1000000)
print(sum(generator))
print(sys.getsizeof(generator))
def fibonacci_generator(limit):
a, b, = 0, 1
while a <= limit:
yield a
a, b = b, a + b
for i in fibonacci_generator(100):
print(i)
print('\n')
print('Generator expressions')
generator = (i for i in range(11) if i % 2 == 0)
for i in generator:
print(i)