-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15 CHAPTER 15 python.py
100 lines (72 loc) · 2.82 KB
/
15 CHAPTER 15 python.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# **************************************************************************************************************************************************************
# *********************************************************CH - 15*************************************************************************************************
# **************************************************************************************************************************************************************
# **************************************************************************************************************************************************************
# **************************************************************************************************************************************************************
# ========================================================= Intro to Generators ======================================================
# generators are iterator
# iterator , iterable
l = [1,2,3] # iterable
# for i in l:
# print(i)
# i = iter(l)
# print(next(i))
# print(next(i))
# print(next(i))
# # print(next(i))
# for i in map(lambda a:a**2, l):
# print(i)
# l = [1,4,9,16]
# memory ---- [..................................................]
# memory ---- (..................................................)
# write first generators
# creat your first generators with Generator function
# this is my first project with genetator function
# 1.) Generator function
# 2.) Generator comprehension
# 10 , 1 to 10
def nums(n):
for i in range(1,n+1):
yield i
# print(nums(10))
for numbers in nums(10):
# print(numbers)
# numbers = list(nums(10))
# for num in numbers:
# print(num)
# for num in numbers:
# print(num)
# ch 15 exercise
# define generators function
# take one num as argument
# Generate a sequence of even num from 1 to that number
def even_generator(n):
for i in range(1,n+1):
if i%2 == 0:
yield(i)
for num in even_generator(20):
print(num)
# for num in even_generator(20):
# print(num)
# for number in even_generator(8):
# print(number)
def even_geneator(n):
for i in range(2,n+1,2):
yield(i)
for num in even_geneator(20):
print(num)
# Generator comprehension
square_list = (i**2 for i in range(1,11))
print(list(square_list))
print(next(square_list))
#
import time list vs generators
# memory used, Time
# when to use list, when to use generators
# t1 = time.time()
# l = [i**2 for i in range(10000000)]
# print(time.time() - t1)
# print(t2-t1)
t1 = time.time()
l = (i**2 for i in range(10000000))
print(time.time() - t1)