PY-101 ยท Functions
Recursion
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก 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.
def countdown(n):if n == 0:print("Lift off!")returnprint(n)countdown(n - 1)countdown(3)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
3
2
1
Lift off! ๐ฏ Write a recursive factorial(n): factorial(0) is 1, and factorial(n) is n * factorial(n - 1). Print factorial(5), which is 120.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
if n == 0: return 1, otherwise return n * factorial(n - 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.
- The Python idea
def f(n):Recursion is when a function calls itself to solve a smaller version of the same problem. - 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)) Real-world scenario
File explorers, website menus with sub-menus and JSON parsers all walk nested structures recursively.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Lists โ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
- Recursion limit (docs.python.org)