Phase 4: Computational Thinking & Algorithms · ~35 minutes · Python · uv
Recursion
A function calling itself isn't a special kind of loop. It's an ordinary function call — which means it can genuinely run out of room, and does.
Hiring signal: Can name the base case and recursive case of any recursive function on sight, and knows exactly why missing one crashes
What you will learn
- Explain recursion as base case + recursive case, not as magic
- Trace a recursive function's execution by hand for a small input
- Reproduce and explain a real RecursionError from a missing base case
- Use recursion on genuinely nested data, where it's the natural tool
Introduction
Type: Learn Languages: Python Prerequisites: Lesson 03 (Sorting) Time: ~35 minutes
Objective
Learning objectives
- Explain recursion as base case + recursive case, not as magic
- Trace a recursive function's execution by hand for a small input
- Reproduce and explain a real
RecursionError from a missing base case - Use recursion on genuinely nested data, where it's the natural tool
What you're building
A script (recursive_tools.py) that:
- Implements a recursive function that sums all numbers in an arbitrarily nested list (structured like
nested above), using the same base-case/recursive-case pattern as count_items - Contains a comment tracing your function by hand for a small input (3-4 numbers, at least one level of nesting), showing each call and what it returns
- Reproduces this lesson's missing-base-case
RecursionError in a separate, clearly-commented-out or guarded block (don't leave it as code that actually runs and crashes the script), and explains in a comment exactly which line is missing - Writes the corrected version alongside it, with a real base case
A recursive function computes the sum of a list using def sum_list(data): return data[0] + sum_list(data[1:]). What's wrong with it?
This function has a real recursive case (data[0] + sum_list(data[1:]), which does shrink data by one element each call, moving toward an eventually-empty list) but genuinely no base case at all — nothing checks whether data is empty and stops. Once data becomes [], data[0] raises an IndexError (list index out of range, from Phase 02) rather than a clean, deliberate stopping point. The fix needs an explicit base case: if not data: return 0, checked before touching data[0] at all.
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The Problem, Check Yourself, Key Terms & Next — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy