fibonacci series yield code example

Example 1: python fibonacci generator

def fib(num):
    a = 0
    b = 1
    for i in range(num):
        yield a
        temp = a
        a = b
        b = temp + b


for x in fib(100):
    print(x)


def fib2(num): # Creates fib numbers in a list
    a = 0
    b = 1
    result = []
    for i in range(num):
        result.append(a)
        temp = a
        a = b
        b = temp + b
    return result


print(fib2(100))

Example 2: python fibonacci sequence generator

number1 = 0
print('1:', number1)
number2 = 1
for count in range(2, 101):
  print(count, ':', number1 + number2)
  number1 += number2
  number2 = number1 - number2