In [1]:
from itertools import islice

def fib():
    prev, curr = 0,1
    while True:
        yield curr #No Return statement
        prev, curr = curr, prev + curr

f = fib()
list(islice(f, 0, 10))
Out[1]:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
In [2]:
numbers = [1, 2, 3, 4, 5, 6]
[x * x for x in numbers]
Out[2]:
[1, 4, 9, 16, 25, 36]
In [3]:
lazy_squares = (x * x for x in numbers)
print(lazy_squares)
print(next(lazy_squares))
print(list(lazy_squares))
<generator object <genexpr> at 0x7f7f086bc0c0>
1
[4, 9, 16, 25, 36]
In [4]:
def something(numbers):
    result = []
    for n in numbers:
        result.append(n ** 2)
    return result

squares = something(numbers)
print(squares)
[1, 4, 9, 16, 25, 36]
In [5]:
def iter_something(numbers):
    for n in numbers:
        yield n ** 2
        
squares = iter_something(numbers)
print(list(squares))
[1, 4, 9, 16, 25, 36]