Tutorials Logic, IN info@tutorialslogic.com

Python Recursion: Base Case, Call Stack, and Examples

Recursive Flow

Recursion means a function calls itself to solve a smaller version of the same problem.

Every recursive function needs a base case that stops the calls and a recursive step that moves closer to that base case.

Use recursion for naturally nested problems. Use loops when the problem is simply repeating over a flat sequence.

Base Case

The base case is the condition that returns an answer without calling the function again.

  • Without a base case, recursion keeps calling itself until Python raises RecursionError.
  • The base case should be easy to find at the top of the function.

Countdown Base Case

Countdown Base Case
def countdown(number):
    if number == 0:
        return

    print(number)
    countdown(number - 1)

countdown(3)
Output
3
2
1

When number reaches 0, the function returns instead of making another recursive call.

Recursive Step

The recursive step calls the same function with a smaller or simpler input.

  • Make the input closer to the base case on every call.
  • Return the recursive result when the function must build a value.

Factorial Function

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

print(factorial(5))
Output
120

factorial(5) waits for factorial(4), and each smaller call eventually reaches factorial(0).

Stack Depth

Each recursive call waits on the call stack. Too many nested calls can hit Python recursion limits.

  • Avoid recursion for very long simple loops.
  • Use recursion carefully for trees, folders, nested menus, and divide-and-conquer logic.

Nested Data

Recursion fits nested structures because each item can be handled with the same rule.

  • Check whether the current value is a simple value or another nested collection.
  • Call the same function for nested items.

Flatten Nested Lists

Flatten Nested Lists
def flatten(items):
    result = []
    for item in items:
        if isinstance(item, list):
            result.extend(flatten(item))
        else:
            result.append(item)
    return result

print(flatten([1, [2, 3], [4, [5]]]))
Output
[1, 2, 3, 4, 5]

The same flatten() function is reused whenever another list appears inside the data.

Skill check

Can You Stop Recursion?

5 checks
  • Write the base case before the recursive call.
  • Make each recursive call move closer to the base case.
  • Return values consistently from every branch.
  • Avoid recursion for huge flat loops.
  • Use small inputs first when debugging recursive flow.

Recursion Stop Conditions

  • Missing the base case

    Write the stopping condition before the recursive call.
  • Not making the problem smaller

    Each recursive call should move closer to the base case.
  • Using recursion where a loop is simpler

    Prefer loops for straightforward repetition in beginner code.

Try this next

Solve a Smaller Case

0 of 2 completed

  1. Write countdown(n) and list each call until the base case.
  2. Mark the base case in factorial, countdown, and tree traversal examples.

Questions About Recursion

The function kept calling itself too many times, usually because the base case was missing or never reached.

Usually not in beginner Python. Choose recursion for clarity with nested problems, not for speed.

Print the input at the start of the function and test the smallest possible input before larger cases.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.