Generators#

topic 1#

Iterable - __iter__ or __getitem__ ...
Iterator - __next__ ...
Iteration - process to iterate over iterable
[1]:
l = [1,2,4,5]
[7]:
for item in dir(l):
    if item in ['__iter__','__getitem__']:
        print(item)
__getitem__
__iter__
[8]:
for i in l:
    print(i)
1
2
4
5
[13]:
def custom_gen(n):
    for i in range(n):
        yield i

for item in dir(custom_gen(10)):
    if item in ['__next__']:
        print(item)
__next__
[23]:
g = custom_gen(5)
[24]:
next(g)
[24]:
0
[25]:
next(g)
[25]:
1
[26]:
next(g)
[26]:
2
[28]:
g = custom_gen(5)
for item in g:
    print(item)
0
1
2
3
4
[29]:
n = "Nishant"
for item in n:
    print(item)
N
i
s
h
a
n
t
[32]:
iterable = iter(n)
[34]:
for item in dir(iterable):
    if item in ['__next__']:
        print(item)
__next__
[35]:
next(iterable)
[35]:
'N'
[36]:
next(iterable)
[36]:
'i'
[37]:
next(iterable)
[37]:
's'
[ ]: