Home » Python Tutorial » Fibonacci series in python

# Fibonacci series in python

Fibonacci series in python is a sequence of numbers in which the current term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 and 1. So what is the logic behind this? Let’s understand it in detail.

### The logic behind Fibonacci sequence in python

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,… this is the Fibonacci sequence. As you can see, the first two terms of the sequence are 0 and 1. The third term is an addition to the first two terms and so on.

In general,

Fn = Fn-1 + Fn-2

Where Fn is a current term.

### Examples of the Fibonacci series program in python

We can implement the Fibonacci series logic in many ways. We will learn with for loop, while loop, recursion, and dynamic memory allocation.

## Fibonacci series in python using for loop

Accept the number of terms from the user and pass that number in the loop and implement the Fibonacci number’s logic in the loop.

Example

``````n = int(input("Enter number of terms: "))
n1, n2 = 0, 1 # first two terms of fibonacci series
i = 0  #variable for loop

if n <= 0:
elif n == 1:
print("Fibonacci sequence upto",n,":")
print(n1)
else:
print("Fibonacci sequence:")
for i in range(n):  #for loop upto nth term
print(n1)
sum = n1 + n2  #Logic for the fibonacci series
n1 = n2
n2 = sum
i += 1``````

Output

``````Enter number of terms: 10
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34``````

## Fibonacci series in python using while loop

Take a number of terms of the Fibonacci series as input from the user and iterate while loop with the logic of the Fibonacci series.

Example

``````n = int(input("Enter number of terms: "))
n1, n2 = 0, 1 # first two terms of fibonacci series
i = 0

if n <= 0:
elif n == 1:
print("Fibonacci sequence upto",n,":")
print(n1)
else:
print("Fibonacci sequence:")
while(i<n): # Exit condition
print(n1)
sum = n1 + n2 #Logic for the fibonacci series
n1 = n2
n2 = sum
i += 1``````

Output

``````Enter number of terms: 10
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34``````

## Fibonacci series using recursion in python

Same logic as above, accept a number from the user and pass it to the function. The function will return the value and outside we print it.

``````def Fibonacci(n): #fibinacci function
if n < 0:
print("Incorrect input")

elif n == 0:
return 0

elif n == 1 or n == 2:
return 1

else:
return Fibonacci(n - 1) + Fibonacci(n - 2) #Logic for the fibonacci series

n = int(input("Enter number of terms: "))

i = 0
for i in range(n):
print(Fibonacci(i))``````

Output

``````Enter number of terms: 10
0
1
1
2
3
5
8
13
21
34``````

## Printing Nth term of Fibonacci series using recursion

Accept a number of a term from the user and pass it to the function. It will return the exact term of the Fibonacci series.

Example

``````def Fib(x): #fibinacci function
if x < 0:
print("Incorrect input")
elif x == 0:
return 0
elif x == 1 or x == 2:
return 1
else:
return Fib(x-1) + Fib(x-2) #Logic for the fibonacci series

x = int(input("Enter number of terms: "))
print(Fib(x))``````

Output

``````Enter number of terms: 10
55``````

## Printing Fibonacci series dynamically

Initialize an array with 0 and 1 and accept a number of terms from the user. The function will append the values to the array which will reduce memory wastage.

Example

``````def fib(n):
a = [0,1]
if n == 1:
print('0')
elif n == 2:
print(a)
else:
while(len(a) < n):
a.append(0)
if(n == 0 or n == 1):
return 1
else:
a=0
a=1
for i in range(2, n):
a[i]=a[i - 1]+a[i - 2]
print("First {} terms of fibonacci series:".format(n),a)
return a[n - 2]

n = int(input("Enter number of term: "))
fib(n)``````

Output

``````Enter number of term: 10
First 10 terms of fibonacci series: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]``````
Shares