GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Recursion in Python - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Recursion in Python

Last Updated : 19 May, 2026

Recursion is a programming technique where a function calls itself either directly or indirectly to solve a problem. It is commonly used for:

  • Breaking problems into smaller subproblems
  • Mathematical calculations like factorial and Fibonacci
  • Tree and graph traversal and Divide-and-conquer algorithms

Working of Recursion

A recursive function calls itself in its body. Let's see basic structure of recursive function:

def recursive_function(parameters):
if base_case_condition:
return base_result
else:
return recursive_function(modified_parameters)

1
Recursion in Python

Recursive function contains two key parts:

  • Base Case: stopping condition that prevents infinite recursion.
  • Recursive Case: part of the function where it calls itself with modified parameters.

Examples

Example 1: This code defines a recursive function to calculate factorial of a number, where function repeatedly calls itself with smaller values until it reaches the base case.

Python
def factorial(n):
    if n == 0:  # Base case
        return 1
    else:       # Recursive case
        return n * factorial(n - 1)

print(factorial(5))

Output
120

Explanation:

  • Base Case: when n == 0, recursion stops and returns 1.
  • Recursive Case: multiplies n with the factorial of n-1 until it reaches the base case.

Example 2: This code defines a recursive function to calculate nth Fibonacci number, where each number is the sum of the two preceding ones, starting from 0 and 1.

Python
def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(10))  

Output
55

Explanation:

  • Base Cases: if n == 0, the function returns 0. If n == 1, the function returns 1. These two cases are necessary to stop the recursion.
  • Recursive Case: function calls itself twice with decrements of n (i.e., fibonacci(n-1) and fibonacci(n-2)), summing results of these calls.

Types of Recursion

Recursion can be broadly classified into two types: tail recursion and non-tail recursion. The main difference between them is related to what happens after recursive call.

  • Tail Recursion: The recursive call is the last thing the function does, so nothing happens after it returns. Some languages can optimize this to work like a loop, saving memory.
  • Non-Tail Recursion: The function does more work after the recursive call returns, so it can’t be optimized into a loop.

Example: This code compares tail recursion and non-tail recursion using two versions of factorial function one with an accumulator (tail-recursive) and one with multiplication after recursive call (non-tail-recursive).

Python
def tail_fact(n, acc=1):
    if n == 0:
        return acc
    else:
        return tail_fact(n-1, acc * n)

def nontail_fact(n):
    if n == 0:
        return 1
    else:
        return n * nontail_fact(n-1)
        
print(tail_fact(5))  
print(nontail_fact(5))  

Output
120
120

Explanation:

  • def tail_fact(n, acc=1): - Defines a tail-recursive factorial function with an accumulator acc to store intermediate results.
  • if n == 0: return acc - Base case: when n reaches 0, return the accumulated result.
  • return tail_fact(n-1, acc * n) - Tail-recursive call: multiplies acc by n before the call, so no extra work is left after recursion.
  • def nontail_fact(n): - Defines a non-tail-recursive factorial function.
  • if n == 0: return 1 - Base case: when n == 0, return 1 (factorial of 0 is 1).
  • return n * nontail_fact(n-1) - Non-tail call: multiplication happens after the recursive call returns, so more work remains after recursion.

When to Avoid Recursion

  • When the problem can be solved easily with loops.
  • When recursion depth is large enough to risk a stack overflow.
  • When performance is critical and function call overhead matters.

Recursion vs Iteration

Recursion and iteration are two common techniques used to repeat tasks in programming below table highlights the key differences between them:

FeatureRecursionIteration
MethodA function calls itself to repeat the taskUses loops (for, while) to repeat steps
Memory UseUses more memory due to function callsUses less memory
PerformanceUsually slower because of function call overheadGenerally faster as it avoids repeated function calls
Best Used ForProblems like tree traversal or divide-and-conquerRepeating steps in a sequence
RiskMay cause stack overflow if recursion is too deepNo stack overflow risk
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.