-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29.2.py
More file actions
43 lines (24 loc) · 1.15 KB
/
29.2.py
File metadata and controls
43 lines (24 loc) · 1.15 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
def printVal(l):
for value in l:
yield value
l = [10,20,30,40,50,60]
g = printVal(l)
print(next(g))# 10
print(next(g))# 20
print(next(g))# 30
print(next(g))# 40
print(next(g))# 50
print(next(g))# 60 untill here it was present in list, what happend now, if i print again the same command
#print(next(g))# StopIteration ( So it's showing that StopIteration ) So, inn case of generator we have to handle it by try and except block, ( while ) in case of normal for loop it indicates atomatically
l = [10,20,30,40,50,60]
l2 = [value * value for value in l]
print(l2)# [100, 400, 900, 1600, 2500, 3600] as we have disccused in list but when this big bracket is converted into parenthesis
l2 = (value * value for value in l)
print(l2)# <generator object <genexpr> at 0x00000000030780C8> now we can get next output one by one by using simple repeating command
print(next(l2))# 100
print(next(l2))# 400
print(next(l2))# 900
print(next(l2))# 1600
print(next(l2))# 2500
print(next(l2))# 3600
# So by the generator we can create a output t a time that's it's a best way to save our meory in python programming language