Python Program to Display Fibonacci Sequence Using Recursion Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report We are given a task to write the Fibonacci sequence using recursion. we will take the range as input of integer and then print the Fibonacci Sequence. In this article, we will see the method of Python Program to Display Fibonacci Sequence Using Recursion. Example: Input: n = 9Output: 0 1 1 2 3 5 8 13 21Explanation: Here, we have n = 9, and we print the Fibonacci Sequence by adding one element with its subsequent element.What is the Fibonacci Sequence?The Fibonacci sequence is like a mathematical melody, where each number dances to the rhythm of the two preceding ones. It usually begins its graceful dance with the numbers 0 and 1. This sequence is not just a mere mathematical curiosity; it's like a versatile performer that appears on the stages of various mathematical and computer science domains. The Fibonacci sequence is defined by the recurrence relation: F(n) = F(n-1) + F(n-2) where ( 0 ) = 0 F(0)=0 and ( 1 ) = 1 F(1)=1. Python Program to Display Fibonacci Sequence Using RecursionBelow, are the implementation of Python Program to Display Fibonacci Sequence Using Recursion. The code defines a recursive function, fib, to generate Fibonacci series.It also contains a function print_fib to handle edge cases and initiate the Fibonacci series printing.The program checks for invalid inputs and prints appropriate messages.It initializes the series with 0 and 1 and calculates subsequent terms by summing the previous two.The driver code calls print_fib with the desired number of Fibonacci series terms, which is set to 9 in this case.Implementation Python3 # Recursive function to print the Fibonacci series def fib(n, prev1, prev2): if n < 3: return fn = prev1 + prev2 prev2 = prev1 prev1 = fn print(fn, end=" ") fib(n - 1, prev1, prev2) def print_fib(n): # When the number of terms is less than 1 if n < 1: print("Invalid number of terms") elif n == 1: print(0) elif n == 2: print("0 1") else: print("0 1", end=" ") fib(n, 1, 0) # Driver code if __name__ == "__main__": n = 9 # Function call print_fib(n) Output0 1 1 2 3 5 8 13 21 Time complexity: O(n),Auxiliary Space: O(n), Create Quiz Comment K kumarwatsal43 Follow 1 Improve K kumarwatsal43 Follow 1 Improve Article Tags : Python Python-DSA Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like