In this lesson
- It starts with a question. Sione writes total(n). It has two lines: “if n == 0: return 0” and “return n + total(n - 1)”. What does total(3) give back?
- The idea you will find. Do one small piece, and trust the smaller call for the rest. — Each call of total did only one thing: it added its own n to the answer from a smaller problem. No call ever saw the whole sum. The base case supplies the first real value, and every waiting call adds its piece as the answers come back up the chain.
- How you learn it. By doing: putting things in order, a slider.
- Length. 6 short steps. Progress saves on this phone.
Part of Python: Recursion
Solving a problem by solving a smaller copy of it — and always leaving a way out.