Python Examples

Python Program to Display Fibonacci Sequence Using Recursion

Let us consider the numbers as 0,1 of the Fibonacci sequence python. The rest of the series can be derived by adding the preceding two numbers together. This means that to find the Fibonacci series of the nth number, you will need the sum of (n-1) and (n-2) terms. Example: The Fibonacci sequence python is as follows 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, etc.

fibonacci sequence python

Source Code

How do you write a Fibonacci Sequence in Python?

To find the Fibonacci sequence python, use the following methods:  

Method 1: Using recursion

Method 2: Using Dynamic Programming 

Method 3: Using Space Optimized technique

Method 4: Using Arrays

Is there a Fibonacci Function in Python?

To find the Fibonacci sequence python, we can use the recursive function. The recursive function in Python allows us to print the n-series in Python. 

                    

Source Code:

def FibRec(a):  

   if a <= 1:  

       return a  

   else:  

       return(FibRec(a-1) + FibRec(a-2))  

 aterms = int(input("Enter the terms? ")) 

  
if aterms <= 0:  

   print("Please enter a positive integer")  

else:  

   print("Fibonacci sequence:")  

   for z in range(aterms):  

       print(FibRec(z))

Output:

Enter the terms?

5

0 1 1 2 3

How do you find the nth Fibonacci Number in Python?

As discussed above, there are four methods to find the nth Fibonacci number in Python. Let us discuss the four methods used to find the Fibonacci series in detail:

Method 1 ( Use recursion ) :

                    

def Fibo(a):

    if a<= 0:

        print("Incorrect input")

#First Fibo number is 0.

    elif a == 1:

        return 0

    # Second Fibo number is 1

    elif a == 2:

        return 1

    else:

        return Fibo(a-1)+Fibo(a-2)

# Driver Program

print(Fibo(0))

Method 2- Using Dynamic Programming:

                    

FiboArray = [0, 1]

def fibonacci(n):

    if a<0:

        print("Incorrect input")

    elif a<= len(FiboArray):

        return FiboArray[a-1]

    else:

        t_fib = fibonacci(a-1)+fibonacci(a-2)

        FiboArray.append(t_fib)

        return t_fib

# Driver Program

print(fibonacci(9))

Output:

21

Method 3- Using Dynamic Programming with Space Optimization

                    

# The first Fibonacci number is 0 and the second Fibonacci number is 1

 def fibo(z):

    a = 0

    b = 1

    if z < 0:

        print("Incorrect input")

    elif z == 0:

        return a

    elif z == 1:

        return b

    else:

        for x in range(2, z):

            c = a + b

            a = b

            b = c

        return b

 # Driver Program

 print(fibonacci(9))

 Output:

21

Using Arrays:

                    

def fibo (a):

   if a<= 0:

     return "Incorrect Output"

   data = [0, 1]

   if a > 2:

        for x in range (2, a):

            data.append(data[x-1] + data[x-2])

    return data[n-1]

# Driver Program

 print(fibonacci(9))

Output:

21

How do you write a Fibonacci Series for a loop in Python?

In Python, loops allow us to execute a group of statements repeatedly. Look at the source code discussed below:

                    

Source Code:

i=int(input("Enter the terms"))

first=0                                        

second=1                                        

if i<=0:

    print("The requested series is

",first)

else:

    print(first,second,end=" ")

    for x in range(2,i):

        n=first+second                          

        print(n,end=" ")

        first=second

        second=n

Output: Enter the terms 

5 

0 

1 

1 

2 

3

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.