list1 = [1,2,3,4]
it = iter(list1)
it
<list_iterator at 0x593d518>
next(it)
1
next(it)
2
next(it)
3
next(it)
4
next(it)
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-10-bc1ab118995a> in <module>()
----> 1 next(it)
StopIteration:
for i in it:
print(i)
1
2
3
4
list_ex = [x*x for x in range(10)]
list_ex
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
generator_ex = (x*x for x in range (10))
generator_ex
<generator object <genexpr> at 0x00000000056D61A8>
def fib(max):
n,a,b=0,0,1
while n < max:
a,b=b,a+b
n += 1
print(a)
return None
fib(10)
1
1
2
3
5
8
13
21
34
55
def fib(max):
n,a,b=0,0,1
while n < max:
yield b
a,b=b,a+b
n += 1
return "None"
a=fib(10)
next(a)
1
next(a)
1
next(a)
2
next(a)
3