fobonacci in python code example
Example 1: fibonacci sequence python
def fib(num):
"""return the number at index `num` in the fibonacci sequence"""
if num <= 2:
return 1
return fib(num - 1) + fib(num - 2)
def fib2(num):
a, b = 1, 1
for _ in range(num - 1):
a, b = b, a + b
return a
print(fib(6))
print(fib2(6))
Example 2: how to create fibonacci sequence in python
n = int(input("Enter the value of 'n': "))
a = 0
b = 1
sum = 0
count = 1
print("Fibonacci Series: ", end = " ")
while(count <= n):
print(sum, end = " ")
count += 1
a = b
b = sum
sum = a + b
Example 3: 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