Lesson 24 of 60
โฎPrev Nextโฏ

PY-101 ยท Functions

Recursion

โฑ 8 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

24of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    Recursion is when a function calls itself to solve a smaller version of the same problem. Every recursive function needs a base case, the simplest input it answers directly, or it would call itself forever. It suits problems that are naturally nested, like folders inside folders.

    • Write a recursive function
    • Always include a base case
    • Know when a loop is simpler

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    Recursion, running
    1. def countdown(n):
    2. if n == 0:
    3. print("Lift off!")
    4. return
    5. print(n)
    6. countdown(n - 1)
    7. countdown(3)
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemTo find every file on a drive, you open a folder, list its files, and for each folder inside it you do the very same thing. That "do the same thing inside" is recursion.
    2. The Python ideadef f(n):Recursion is when a function calls itself to solve a smaller version of the same problem.
    3. In real lifeFile explorers, website menus with sub-menus and JSON parsers all walk nested structures recursively.

    Start with a question

    To find every file on a drive, you open a folder, list its files, and for each folder inside it you do the very same thing. That "do the same thing inside" is recursion.

    Tree-shaped data (folders, menus, JSON, family trees) is everywhere, and recursion is the natural way to walk it.

    Explain it like I'm 10

    Russian dolls: to find the smallest doll, open the doll, and if there is another doll inside, do the same again. You stop when a doll has nothing inside.

    The technical explanation

    The base case answers the simplest input without recursing, for example if n == 0: return 1.

    The recursive case calls the function with a smaller input: return n * factorial(n - 1).

    Each call waits for the one below it, then finishes in reverse order. Watch it in the scene above.

    Syntax

    def f(n):
        if base_case:
            return answer
        return combine(n, f(smaller_n))

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    File explorers, website menus with sub-menus and JSON parsers all walk nested structures recursively.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What is recursion?
    2. 2. What stops recursion?
    3. 3. What happens with no base case?
    4. 4. def f(n): return 0 if n == 0 else n + f(n - 1). f(3)?
    5. 5. Best fit for recursion?

    Questions people ask

    Is recursion better than loops in Python?

    Not usually for simple repetition; loops are faster and have no depth limit. Recursion shines for nested, tree-like data.

    What is the recursion limit in Python?

    About 1,000 nested calls by default. sys.getrecursionlimit() shows it. Hitting it raises RecursionError.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner Is recursion better than loops in Python?

    Not usually for simple repetition; loops are faster and have no depth limit. Recursion shines for nested, tree-like data.

    beginner What is the recursion limit in Python?

    About 1,000 nested calls by default. sys.getrecursionlimit() shows it. Hitting it raises RecursionError.

    intermediate What is recursion?

    A function calling itself. The function calls itself on a smaller problem.

    intermediate What stops recursion?

    The base case. The base case returns without recursing.

    Quick revision

    Remember

    • Write a recursive function
    • Always include a base case
    • Know when a loop is simpler
    • Python stops after about 1,000 nested calls with RecursionError. For long counts, a loop is the better tool.

    Syntax

    def f(n):
        if base_case:
            return answer
        return combine(n, f(smaller_n))

    Official documentation