tech beamers
  • Python Lab
    • Python Online Compiler
    • Python Code Checker
    • Python Coding Exercises
    • Python Coding Quizzes
  • SQL Practice
  • Selenium Practice
  • SW Guides
tech beamers
Search
  • Python Lab
    • Python Online Compiler
    • Python Code Checker
    • Python Coding Exercises
    • Python Coding Quizzes
  • SQL Practice
  • Selenium Practice
  • SW Guides
Follow US
© TechBeamers. All Rights Reserved.
Python Exercises

Python Program: Generate Fibonacci using Recursion

Last updated: Nov 30, 2025 12:05 pm
Meenakshi Agarwal
By Meenakshi Agarwal
No Comments
2 months ago
SHARE

In this sample program, you will learn how to generate a Fibonacci sequence using recursion in Python and show it using the print() function.

Contents
  • Generate a Fibonacci sequence Using Recursion
    • Fibonacci Program in Python
    • Summary
Python program to Generate Fibonacci Sequence using Recursion

Generate a Fibonacci sequence Using Recursion

To understand this demo program, you should have basic Python programming knowledge. Also, you can refer our another post to generate a Fibonacci sequence using a while loop.

Fibonacci Program in Python

However, here we’ll use the following steps to produce a Fibonacci sequence using recursion.

Steps to generate the Fibonacci series

  1. Get the length of the Fibonacci series as input from the user and keep it inside a variable.
  2. Send the length as a parameter to our recursive method which we named as the gen_seq().
  3. The function first checks if the length is lesser than or equal to 1.
  4. If the length is less or equal to 1, then it returns immediately.
  5. In other cases, it makes two adjoining recursive calls with arguments as (length-1) and (length-2) to the gen_seq() function.
  6. We are calling the recursive function inside a for loop which iterates to the length of the Fibonacci sequence and prints the result.

Below is the sample code of the Python Program to evaluate the Fibonacci sequence using recursion.

Recursion code to generate Fibonacci sequence in Python

You can use IDLE or any other Python IDE to create and execute the below program.

# Program to generate the Fibonacci sequence using recursion

def gen_seq(length):
    if(length <= 1):
        return length
    else:
        return (gen_seq(length-1) + gen_seq(length-2))

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

print("Fibonacci sequence using Recursion :")
for iter in range(length):
    print(gen_seq(iter))

The output of the above code is as follows.

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

Now, you can extend this program to measure the execution time required to generate the sequence. You can use the Python time functions for this purpose.

Summary

We hope the above program would have simplified the recursion logic to generate the Fibonacci sequence for you. However, feel free to ask any query or share if you can do this differently.

Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.

Enjoy Coding,
TechBeamers

Share This Article
Whatsapp Whatsapp LinkedIn Reddit Copy Link
Meenakshi Agarwal Avatar
ByMeenakshi Agarwal
Follow:
I’m Meenakshi Agarwal, founder of TechBeamers.com and ex-Tech Lead at Aricent (10+ years). I built the Python online compiler, code checker, Selenium labs, SQL quizzes, and tutorials to help students and working professionals.
Previous Article Python program to generate a Fibonacci sequence Python Program: 6 Ways to Generate Fibonacci Sequence
Next Article Python Program To Convert Lists Into A Dictionary Python Program: Convert Lists into a Dictionary
Leave a Comment

Leave a Reply

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

Most Popular This Month

  • → Python Online Compiler
  • → Python Code Checker
  • → Free Python Tutorial
  • → SQL Practice Queries
  • → Code to Flowchart Tool
  • → Python Syntax Guide
  • → Python List & Dict Questions
  • → Selenium Practice Test Page

RELATED TUTORIALS

Convert Python list to string with examples

Python Program: Convert a List to String

By Meenakshi Agarwal
2 months ago
Check If Python List Contains Elements Of Another List

Python Program: Check List Contains Another List Items

By Meenakshi Agarwal
2 months ago
45 Python Exercises on Loops, Conditions, and Range() Function

Python Control Flow Exercises

By Meenakshi Agarwal
9 months ago
Python program to generate a Fibonacci sequence

Python Program: 6 Ways to Generate Fibonacci Sequence

By Meenakshi Agarwal
2 months ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use