-
Notifications
You must be signed in to change notification settings - Fork 0
/
video8.py
80 lines (47 loc) · 1.52 KB
/
video8.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
''' Topics : generator, generator expression, generator function,
advantages of generator over list '''
# what is generator ?
class my_own_iterator:
"""Class about an iterator which performs i ** i for all i <= user_input"""
def __init__(self, max = 0):
self.max = max
def __iter__(self):
self.ele = 1
return self
def __next__(self):
if self.ele <= self.max:
result = self.ele ** self.ele
self.ele += 1
return result
else:
raise StopIteration
my_itr_obj = my_own_iterator(5)
my_itr = iter(my_itr_obj)
# for ele in my_itr:
# print(ele)
# generator expression / generator comprehension
my_gen = (i ** i for i in range(6) if i > 0)
for ele in my_gen:
print(ele)
# Basic syntax : (expression for var in iterable if var satisfies a condition)
# generator function
def my_gen_fun(max = 0):
num = 1
while num <= max:
yield num ** num
num += 1
my_gen_obj = my_gen_fun(5)
print(my_gen_obj)
for ele in my_gen_obj:
print(ele)
def my_special_gen():
num = 1
while True:
yield num
num += 1
# for ele in my_special_gen():
# print(ele)
# iter(callable, sentinel= some_val)
my_inf_seq = (ele for ele in iter(int, 1)) # iter() returns an infinite iterator
for ele in my_inf_seq:
print(ele)