I'm wondering whether you meant "recursive". Here is a simple example of a recursive function to compute the factorial function:

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

The two key elements of a recursive algorithm are:

  • The termination condition: n == 0
  • The reduction step where the function calls itself with a smaller number each time: factorial(n - 1)
Answer from Greg Hewgill on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ recursion-in-python
Recursion in Python - GeeksforGeeks
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).
Published ย  1 week ago
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ recursion
Python Recursion (Recursive Function)
Every recursive function must have a base condition that stops the recursion or else the function calls itself infinitely. The Python interpreter limits the depths of recursion to help avoid infinite recursions, resulting in stack overflows.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_function_recursion.asp
Python Function Recursion
In this example, tri_recursion() is a function that we have defined to call itself ("recurse"). We use the k variable as the data, which decrements (-1) every time we recurse. The recursion ends when the condition is not greater than 0 (i.e.
๐ŸŒ
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu โ€บ notebooks โ€บ chapter06.01-Recursive-Functions.html
Recursive Functions โ€” Python Numerical Methods
We also have this interactive book ... or Amazon! < CHAPTER 6. Recursion | Contents | 6.2 Divide and Conquer > A recursive function is a function that makes calls to itself....
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_recursion.asp
Python Recursion
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... Recursion is when a function calls itself....
๐ŸŒ
Nanyang Technological University
libguides.ntu.edu.sg โ€บ python โ€บ recursivefunctions
2.7 Recursive Functions - Python for Basic Data Analysis - LibGuides at Nanyang Technological University
Start your data science journey with Python. Learn practical Python programming skills for basic data manipulation and analysis. ... Recursive functions are functions that calls itself.
Top answer
1 of 4
82

I'm wondering whether you meant "recursive". Here is a simple example of a recursive function to compute the factorial function:

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

The two key elements of a recursive algorithm are:

  • The termination condition: n == 0
  • The reduction step where the function calls itself with a smaller number each time: factorial(n - 1)
2 of 4
10

Recursion in Python works just as recursion in an other language, with the recursive construct defined in terms of itself:

For example a recursive class could be a binary tree (or any tree):

class tree():
    def __init__(self):
        '''Initialise the tree'''
        self.Data = None
        self.Count = 0
        self.LeftSubtree = None
        self.RightSubtree = None

    def Insert(self, data):
        '''Add an item of data to the tree'''
        if self.Data == None:
            self.Data = data
            self.Count += 1
        elif data < self.Data:
            if self.LeftSubtree == None:
                # tree is a recurive class definition
                self.LeftSubtree = tree()
            # Insert is a recursive function
            self.LeftSubtree.Insert(data)
        elif data == self.Data:
            self.Count += 1
        elif data > self.Data:
            if self.RightSubtree == None:
                self.RightSubtree = tree()
            self.RightSubtree.Insert(data)

if __name__ == '__main__':
    T = tree()
    # The root node
    T.Insert('b')
    # Will be put into the left subtree
    T.Insert('a')
    # Will be put into the right subtree
    T.Insert('c')

As already mentioned a recursive structure must have a termination condition. In this class, it is not so obvious because it only recurses if new elements are added, and only does it a single time extra.

Also worth noting, python by default has a limit to the depth of recursion available, to avoid absorbing all of the computer's memory. On my computer this is 1000. I don't know if this changes depending on hardware, etc. To see yours :

import sys
sys.getrecursionlimit()

and to set it :

import sys #(if you haven't already)
sys.setrecursionlimit()

edit: I can't guarentee that my binary tree is the most efficient design ever. If anyone can improve it, I'd be happy to hear how

Find elsewhere
๐ŸŒ
Real Python
realpython.com โ€บ python-recursion
Recursion in Python: An Introduction โ€“ Real Python
October 21, 2023 - In Python, itโ€™s also possible for a function to call itself! A function that calls itself is said to be recursive, and the technique of employing a recursive function is called recursion.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ recursion-in-python
Recursion in Python: Concepts, Examples, and Tips | DataCamp
April 9, 2025 - This stack keeps track of all function calls and their variables. When the base case is reached, the stack begins resolving each call in reverse order, computing the final result step by step. Understanding this stack behavior is crucial because it explains both the power and limitations of recursion in Python.
๐ŸŒ
Real Python
realpython.com โ€บ python-thinking-recursively
Thinking Recursively in Python โ€“ Real Python
November 27, 2023 - The algorithm for recursive present delivery implemented in Python: ... houses = ["Eric's house", "Kenny's house", "Kyle's house", "Stan's house"] # Each function call represents an elf doing his work def deliver_presents_recursively(houses): # Worker elf doing his work if len(houses) == 1: house = houses[0] print("Delivering presents to", house) # Manager elf doing his work else: mid = len(houses) // 2 first_half = houses[:mid] second_half = houses[mid:] # Divides his work among two elves deliver_presents_recursively(first_half) deliver_presents_recursively(second_half)
๐ŸŒ
Medium
jasondeden.medium.com โ€บ recursive-functions-in-python-a-visual-walk-through-28cf22cc10e2
Recursive Functions in Python โ€” A Visual Walk-Through | by Jason Eden | Medium
November 3, 2021 - Recursive functions in Python, because it is an interpreted language and will run one line of code at a time by default, will finish one โ€œsideโ€ of the function completely to the bottom first, and then loop back up, iterating at each point up and down the logical tree until youโ€™ve finished all processing (reached a stop criteria) for all nodes and returned to the root node.
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python basics โ€บ python recursive functions
Python Recursive Functions
March 26, 2025 - The following fn() function is a recursive function because it has a call to itself: def fn(): # ... fn() # ... Code language: Python (python)
๐ŸŒ
QuantInsti
blog.quantinsti.com โ€บ recursive-functions-python
Recursive Functions in Python: Concepts, Types, and Applications in Trading
December 9, 2024 - A recursive function in Python programming is a function that calls itself during its execution. This allows the function to repeat itself until it reaches a base case, which is a condition that stops the recursion.
๐ŸŒ
Python Course
python-course.eu โ€บ advanced-python โ€บ recursive-functions.php
1. Recursive Functions | Advanced | python-course.eu
Enjoying this page? We offer live Python training courses covering the content of this site. ... Recursion is a method of programming or coding a problem, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_recursion.htm
Python - Recursion
This technique breaks down a complex problem into smaller and more manageable sub-problems of the same type. In Python, recursion is implemented by defining a function that makes one or more calls to itself within its own body.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ recursion
Python Recursion: Syntax, Usage, and Examples
Python recursion is a technique where a function calls itself to solve a problem in a step-by-step manner. It is commonly used for solving problems that can be broken down into smaller subproblems, such as calculating factorials, Fibonacci sequences, and performing binary search.
๐ŸŒ
Invent with Python
inventwithpython.com โ€บ blog โ€บ 22-examples-of-recursive-functions.html
22 Examples of Recursive Functions in Python - Invent with Python
October 4, 2021 - Here are 22 actual, runnable Python code for several recursive functions, written in a style to be understandable by beginners and produce debuggable output.