• Python

Command Palette

Search for a command to run...

Python Fundamentals
  • Python Variables
  • Python Operators
  • Python Input-Output
  • Python Type Conversion
Python Data Types
  • Python Strings
  • Python List
  • Python Tuple
  • Python Dictionnaries
  • Python Sets
Python Flow Control
  • Python Conditions
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
Python Functions
  • Python Functions
  • Python Arguments
  • Python Functions Scope
  • Python Recursion
Python Classes
  • Python Classes
  • Python Classes and Static Methods
  • Python Properties
  • Python Decorators
  • Python Error Handling

Create an Account

FREE

Join our community to access more courses.

Create Account

On this page

Solving Problems by Calling Itself: Understanding RecursionWhat is Recursion?Syntax and a Simple ExampleA Classic Example: FactorialVisualizing the Call StackCommon Use CasesRecursion vs. IterationHandling Recursion LimitsSummary
      • Pricing
      • Blog

      Python Recursion

      Solving Problems by Calling Itself: Understanding Recursion

      Recursion is a programming concept where a function solves a problem by calling itself. This technique is often used for tasks that can be naturally broken down into smaller, identical sub-problems.

      Instead of using loops to repeat code a fixed number of times or while a condition is true, recursion repeats a process by having a function call itself with a slightly simpler version of the original problem.

      What is Recursion?

      Recursion occurs when a function calls itself within its own definition. To prevent a function from calling itself indefinitely (which would lead to an error called a "stack overflow"), every recursive function must have two key parts:

      • Base Case: A condition that tells the function when to stop calling itself. When the base case is met, the function performs its final action and returns a result without making another recursive call.
      • Recursive Case: The part where the function calls itself with an input that is a step closer to the base case. This call processes a smaller or simpler version of the original problem.

      A recursive function must have a base case. Without it, the function would call itself infinitely, eventually crashing the program.

      Syntax and a Simple Example

      The basic structure of a recursive function involves an if statement to check for the base case and a call to the function itself within the else (or another conditional) block.

      Here is a simple recursive function that counts down from a given number to zero:

      def countdown(n):
          # Base Case: Stop when n reaches 0
          if n <= 0:
              print("Countdown finished!")
          else:
              # Recursive Case: Print n and call countdown with n-1
              print(n)
              countdown(n - 1) # Recursive call
      

      Output:

      5
      4
      3
      2
      1
      Countdown finished!
      

      In this example, if n <= 0: is the base case. When n is not 0 or less, the function prints n and then calls countdown(n - 1), moving towards the base case.

      A Classic Example: Factorial

      The factorial of a non-negative integer n, denoted as n!, is the product of all positive integers less than or equal to n. The definition is: n! = n * (n-1) * (n-2) * ... * 1

      The base cases are 0! = 1 and 1! = 1.

      This has a natural recursive structure because n! = n * (n-1)! for n > 1. We can implement this recursively in Python:

      def factorial(n):
          # Base Case: Factorial of 0 or 1 is 1
          if n == 0 or n == 1:
              return 1
          else:
              # Recursive Case: n! = n * (n-1)!
              return n * factorial(n - 1) # Recursive call
      

      Example usage:

      print(f"Factorial of 5 is: {factorial(5)}")
      print(f"Factorial of 0 is: {factorial(0)}")
      

      Output:

      Factorial of 5 is: 120
      Factorial of 0 is: 1
      

      Visualizing the Call Stack

      Each time a function is called (including recursive calls), Python creates a new "stack frame" on the call stack to manage the function's local variables and the point to return to. For factorial(3):

      • factorial(3) is called. n=3. n == 0 or n == 1 is false. It needs factorial(2). A frame for factorial(3) waits.
      • factorial(2) is called. n=2. n == 0 or n == 1 is false. It needs factorial(1). A frame for factorial(2) waits.
      • factorial(1) is called. n=1. n == 0 or n == 1 is true. It returns 1. The frame for factorial(1) is removed.
      • The frame for factorial(2) resumes. It receives 1 from factorial(1). It calculates 2 * 1 and returns 2. The frame for factorial(2) is removed.
      • The frame for factorial(3) resumes. It receives 2 from factorial(2). It calculates 3 * 2 and returns 6. The frame for factorial(3) is removed.

      The final result, 6, is returned to the original caller.

      Common Use Cases

      Recursion is often a good fit for problems that match its recursive nature:

      • Tree and Graph Traversal: Navigating through hierarchical data structures.
      • Mathematical Functions: Factorial, Fibonacci sequence (though the naive recursive version is inefficient), power functions.
      • Divide and Conquer Algorithms: Algorithms that break a problem into smaller sub-problems, solve them, and combine the results (e.g., Merge Sort, Quick Sort).
      • Backtracking Algorithms: Used for solving problems by trying to build a solution incrementally, and undoing the last step if it doesn't lead to a valid solution (e.g., solving mazes, N-Queens problem).

      Recursion vs. Iteration

      Any problem that can be solved recursively can also be solved iteratively using loops (for or while) and often an explicit stack data structure.

      Choose recursion when:

      • The problem is inherently recursive (like processing nested data structures or mathematical definitions).
      • The recursive solution is significantly simpler and more readable than an iterative one.

      Choose iteration when:

      • Performance and memory efficiency are critical (recursive calls add overhead to the call stack).
      • The recursion depth might become very large, potentially exceeding Python's recursion limit.

      Handling Recursion Limits

      Python has a default maximum depth for recursive calls to prevent infinite recursion from using up all available memory and crashing the system (Stack Overflow error). You can check this limit:

      import sys
      print(f"Default recursion limit: {sys.getrecursionlimit()}")
      

      You can increase this limit, but do so with caution, as it can still lead to crashes if your recursion is too deep:

      # Use with caution!
      # sys.setrecursionlimit(2000)
      

      For deeply recursive problems, an iterative solution is often safer.

      Summary

      • Recursion is when a function calls itself to solve a problem.
      • Every recursive function needs a base case (to stop) and a recursive case (to call itself with a simpler input).
      • Recursion works well for problems that break down into smaller, similar sub-problems.
      • It uses the call stack to manage function calls.
      • It can sometimes lead to more elegant code for certain problems but might be less efficient or hit Python's recursion depth limit compared to iterative solutions.

      Understanding recursion provides a different perspective on problem-solving and is a valuable technique for certain types of tasks.

      Continue Learning

      Python Variables

      Popular

      Getting Started: Understanding Variables in Python In programming, we often need to store informatio

      Python Classes

      For You

      Building with Blueprints: Understanding Classes and Objects In Python, and many other programming la

      Python Error Handling

      For You

      Making Your Programs Robust: Understanding Errors and Exceptions In Python, errors can happen for ma

      Personalized Recommendations

      Log in to get more relevant recommendations based on your reading history.