tech beamers
  • Viral Tips 🔥
  • Free CoursesTop
  • TutorialsNew
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
    • Python
    • C
    • Java
    • MySQL
    • Linux
    • Web
    • Android
    • AngularJS
    • Playwright
    • Selenium
    • Agile
    • Testing
    • Automation
    • Best IDEs
    • How-To
    • Technology
    • Gaming
    • Branding
  • Interview & Quiz
    • SQL Interview
    • Testing Interview
    • Python Interview
    • Selenium Interview
    • C Sharp Interview
    • Java Interview
    • Web Development
    • PHP Interview
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
    • Python Interview
    • SQL Query Interview
    • SQL Exercises
    • Selenium Interview
    • Playwright Interview
    • QA Interview
    • Manual Testing
    • Rest API Interview
    • Linux Interview
    • CSharp Interview
    • Python Function Quiz
    • Python String Quiz
    • Python OOP Quiz
    • Python DSA Quiz
    • ISTQB Quiz
    • Selenium Quiz
    • Java Spring Quiz
    • Java Collection Quiz
    • JavaScript Quiz
    • Shell Scripting Quiz
  • ToolsHot
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
    • Online Python Compiler
    • Python Code Checker
    • Python Code Quality
    • Username Generator
    • Insta Password Generator
    • Google Password Generator
    • Free PDF Merger
    • QR Code Generator
    • Net Worth Calculator
tech beamers
Search
  • Viral Tips 🔥
  • Free CoursesTop
  • TutorialsNew
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Interview & Quiz
    • SQL Interview
    • Testing Interview
    • Python Interview
    • Selenium Interview
    • C Sharp Interview
    • Java Interview
    • Web Development
    • PHP Interview
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHot
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
Follow US
© TechBeamers. All Rights Reserved.
Python Examples

Python Program: Generate Fibonacci using Recursion

Last updated: Apr 14, 2025 12:26 am
Meenakshi Agarwal
By
Meenakshi Agarwal
Meenakshi Agarwal Avatar
ByMeenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
Follow:
No Comments
5 months ago
Share
3 Min Read
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

Related

Share This Article
Whatsapp Whatsapp LinkedIn Reddit Copy Link
Leave a Comment

Leave a Reply

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

List of Topics

Stay Connected

FacebookLike
XFollow
YoutubeSubscribe
LinkedInFollow

Continue Reading

  • Python Program: Convert Lists into a DictionaryNov 1
  • Python Program: Insert Key-Value Pair in a DictionaryNov 13
  • Python Program: When to Prefer Yield Over ReturnDec 7
  • Python Program: Learn to Build an IRC BotApr 3
  • Python Program: For Loop ExamplesApr 24
  • Python Program to Find Sum of Two NumbersOct 9
  • Python Program: Swap Two Numbers Without a TempOct 10
  • Python Program: Generate Random IntegerOct 17
  • Python Program: Check List Contains Another List ItemsOct 21
  • Python Program: 6 Ways to Generate Fibonacci SequenceOct 30
View all →

RELATED TUTORIALS

How to sort a list of numbers or integers in Python.

Python Program: How to Sort a List of Numbers

By Soumya Agarwal
1 year ago
Python Sort List of Strings With Examples

Python Program: How to Sort List of Strings

By Soumya Agarwal
1 year ago
Count frequency of each word in a Python string

Python Program: Compute Frequency of Each Word

By Meenakshi Agarwal
1 year ago
Program to Reverse a Number using While Loop, Slicing, and Recursion

Python Program to Reverse a Number

By Harsh S.
7 months ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use