Lecture notes · Programming in Python · Lecture 15

Recursion

MIT 6.100L Intro to CS and Programming using Python, lecture 15, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part introduces recursion, a way to solve a problem by breaking it into a smaller version of the same problem, and compares it with the loop-based way of multiplying two numbers.

Key ideas

Words to know

recursion
A way of solving a problem by breaking it into a smaller problem of the same kind, and solving that smaller one the same way.
iterative algorithm
A method that solves a problem by repeating steps again and again inside a loop.
loop
A part of code that repeats the same steps over and over, such as a for loop or a while loop.
state of the computation
The current values of the variables that keep track of progress, which change each time through a loop.

Example

Imagine you need to count 4 bags with 5 apples in each bag. You could count 5 apples four times, which is like the loop, or you could say 5 apples plus the answer for 3 bags, and then repeat that until you reach 1 bag, which is just 5 apples. This is the same idea as breaking 5 times 4 into 5 plus 5 times 3.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part shows how a function can solve a problem by calling itself on a smaller version of the same problem, and how it builds the answer back up.

Key ideas

Words to know

recursion
Solving a problem by having a function call itself on a smaller version of the same problem.
base case
The simple case that can be answered directly, which tells the function when to stop calling itself. For multiplication, it is when b is 1.
recursive step
The part of the function where it calls itself on a smaller input, such as b minus 1, moving closer to the base case.
infinite recursion
A function that keeps calling itself and never reaches a stopping point, so it never finishes.

Example

Think of a line of people where each person can only answer a question by asking the person in front of them, until the first person knows the answer. Once that first person answers, each person in line passes the answer back down the line, adding their own piece as they go. The first person is the base case, and the passing back is how the answer is built up.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part explains recursion, where a function calls itself on a smaller version of the problem until it reaches a simple stopping point, then builds the answer back up.

Key ideas

Words to know

recursion
A way of solving a problem where a function calls itself on a smaller version of the same problem.
base case
The simple situation where the function stops calling itself and gives a direct answer, such as when the power is 0.
iteration
Repeating the same steps over and over, usually with a loop, rather than having a function call itself.
infinite recursion
A function that keeps calling itself forever and never reaches a stopping point.

Example

Imagine you ask a line of people to add up a list of numbers. Each person asks the next one to add their part, until the last person answers. Then each person adds their number to the answer they got back and passes the total back along the line.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part shows how a function that calls itself, called recursion, works step by step using factorial as the example.

Key ideas

Words to know

Base case
The simplest case that the function already knows the answer to, so it can stop calling itself.
Recursive step
The part of the function where it calls itself on a smaller version of the problem.
Environment
A private space for one function call that holds its own variables, like its own n value.
Return value
The answer a function sends back to whoever called it.

Example

Imagine a line of people where each person must ask the next person a question, and the last person in line knows the answer right away. That last person passes the answer back, and each person in turn passes it back down the line until the first person has it. Each person keeps their own copy of the question, just like each function call has its own environment.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–45:06

This part explains how recursion works step by step, and when a recursive solution is easier to write than a loop.

Key ideas

Words to know

recursion
A way of solving a problem where a function calls itself to handle a smaller version of the same problem.
base case
The stopping point in a recursive function, where the function stops calling itself and starts giving answers back.
environment
The private set of variables that belongs to one specific function call, so it does not get mixed up with other calls.
iteration
Repeating a task over and over using a loop, such as a for loop or a while loop, instead of calling a function again.

Example

Think of looking for a file in a set of nested folders. You check the current folder, and if the file is not there, you repeat the same search inside each subfolder, and so on until you find it. You do not need to know in advance how deep the file is buried, which is why this kind of search suits recursion.

3 checked practice questions for this part are in the app.

← Lecture 14: Dictionaries · Lecture 17: Python Classes →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York