-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp15.py
More file actions
277 lines (141 loc) · 5.27 KB
/
p15.py
File metadata and controls
277 lines (141 loc) · 5.27 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Generators
# generators are iterators
# Iterator and iterable
# iterable
l=[1,2,3] # iterable---->list, tuple, string
# for i in l:
# print(i)
i=iter(l) # when for loop run first time, it converts list in iterator by using iter function, and then we have to use next function
print(next(i)) # o/p: 1
print(next(i)) # o/p: 2
print(next(i)) # o/p: 3
# we can change iterable in iterator using iter() function and we can do work as for loop using next() function.
# iterator
s=map(lambda s:s**2,l) # it is iterator
print(s) # o/p: <map object at 0x01639268> # here, we are getting location as output because map is not converted in list.
# map function is also iterator so we don't need to convert in iterator using iter() function. hence, we can directly apply next() function.
print(next(s)) # o/p: 1
print(next(s)) # o/p: 4
print(next(s)) # o/p: 9
for num in map(lambda s:s**2,l): # we can apply for loop in iterator and iterable both.
print(num)
# o/p: 1
# 4
# 9
# generator
# generator is sequence as list but it is not iterable, in fact it is iterator.
# why should we use generator?
# 1). -----In List---
# [.............many numbers........]-----> this huge list will be stored in my memory
# this process will take time because, firstly list will be created, and then it will be stored in memory. as a result this process will consume time and memory both.
#
# 2). -----In generator-----
# one number generate at a time in memory, and when second number will be generated, first number will be gone outside(deleted). (3) or (5), (9)......
# As a result we can save our memory and time.
# when we have to use our sequence many times, we should use list,
# while, we have to use our sequence only for one time, we can use generator. because in generator our sequence is not stored in memory while in list, the sequence stored in memory.
# create first generator with generator function
# we can create generator by using
# 1). generator function
# 2). generstor comprehension
# generator function
def nums(n):
for i in range(1,n+1):
print(i)
nums(10)
# o/p: 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# 10
# we can create generator by using yield keyword
def nums1(n):
for i in range(1,n+1):
# yield(i) # yield is a keyword so you don't have to write i in parenthesis because it's not a function, I mean you can also write as 'yield i' instead of 'yiels(i)'
yield i
nums1(10) # o/p:
print(nums1(10)) # o/p: <generator object nums1 at 0x03A10E98>
for i in nums1(10):
print(i)
# o/p: 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9
# 10
j=nums1(10) # if i will convert this in list like j=list(nums1(10)) i will get output in both below for loop., and j=list(nums1(10)) will also loss the featurs of generator because it has become list.
for i in j:
print(i) # o/p: no 1 to 10
for i in j:
print(i) # o/p: here is no output because nums1() is generator, hence we can can use value only one time.
# -------- Excercise 1 ---------
# define generator function
# take one number as argument
# generate a sequence of even numbers from 1 to that number
def seq_even(n):
for i in range(1,n+1): # for i in range(2,n+1,2):
if i%2==0: # yield i
yield i
s1=seq_even(20)
for i in s1:
print(i)
# o/p: 2
# 4
# 6
# 8
# 10
# 12
# 14
# 16
# 18
# 20
# Generator comprehension
square=[i**2 for i in range(1,11)]
print(square) # o/p: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
generator_square=(i**2 for i in range(1,11)) # here we just use () instead of [] for making generator comprehension
print(generator_square) # o/p: <generator object <genexpr> at 0x033B1E98>
for i in generator_square:
print(i)
# o/p: 1
# 4
# 9
# 16
# 25
# 36
# 49
# 64
# 81
# 100
for i in generator_square:
print(i) # o/p:
# List vs Generator
# memory use, time
# when to use list, when to use generator
# Memory
l=[i**2 for i in range(10000000)] # open task manager and check memory during run mode
l1=(i**2 for i in range(10000000)) #open task manager and check memory during run mode
# in task manager we can easily see the difference of memory occuption.....list will get more memory than generator
# Time
import time
t1=time.time()
l2=[i**2 for i in range(10000000)]
t2=time.time()
print(t2-t1) # o/p: 5.533963918685913
import time
t1=time.time()
l2=(i**2 for i in range(10000000))
t2=time.time()
print(t2-t1) # o/p: 0.0
# when use list
# when we want to store our sequence in memory, and use data more than one time
# when use generator
# when we don't want to store our sequence in memory, and use data only one time