Python Tutorial 0/52 lessons ~6 min read Lesson 14

    Recursion

    Recursion — a function that calls itself — is natural for tree/graph traversal, divide-and-conquer, and parsing.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Recursion — a function that calls itself — is natural for tree/graph traversal, divide-and-conquer, and parsing. Python's default recursion limit is 1000 frames.

    Understanding the topic

    Core concepts to understand:

    • Always have a base case.
    • Each recursive call must move toward the base.
    • sys.setrecursionlimit can raise the cap.
    • Use @functools.lru_cache for memoization.

    Syntax reference

    Visual flow / code:

    python
    from functools import lru_cache
    @lru_cache(maxsize=None)
    def fib(n: int) -> int:
    if n < 2:
    return n
    return fib(n - 1) + fib(n - 2)
    print(fib(50)) # instant with memoization

    Execution workflow

    1Recursion Workflow
    1 / 4

    Always have a base case

    Always have a base case.

    Real-world use

    Memoized recursion turns exponential algorithms (naive fib) into linear ones — a one-line decorator. Standard tool in dynamic programming.

    Best practices

    • Always define the base case first.
    • Add @lru_cache for overlapping subproblems.
    • Prefer iteration when depth is huge.

    Common mistakes

    • Missing base case → RecursionError.
    • Python has no tail-call optimization.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1IntermediateQuestionWhat's tail recursion?+

    Answer

    Tail recursion is when the recursive call is the very last action of a function, so no work remains after it returns. In languages with tail-call optimization the compiler can reuse the current stack frame and avoid stack growth. CPython does not do this, so tail-recursive functions still consume a frame per call.

    Follow-up

    How would you rewrite a tail-recursive function in Python?
    2IntermediateQuestionWhy does Python lack TCO?+

    Answer

    CPython does not implement tail-call optimization by design. Guido van Rossum has said it would remove frames from tracebacks, hurting debugging, and that Python favors explicit iteration instead. The practical consequence is a default recursion limit (see sys.getrecursionlimit(), usually 1000), after which you get RecursionError, so deep recursion should be converted to loops or an explicit stack.
    3IntermediateQuestionWhat is memoization?+

    Answer

    Memoization caches the result of a function for given arguments so repeated calls with the same input return the stored value instead of recomputing. In Python the easiest way is functools.cache or lru_cache, which require hashable arguments. It turns an exponential recursion like naive Fibonacci into linear time at the cost of memory.

    Follow-up

    What is the difference between cache and lru_cache(maxsize=...)?

    Summary

    In summary: Base case + smaller problem. lru_cache supercharges recursion.

    Ready to mark this lesson complete?Track your journey across the entire course.