Python (Fall 2026) Module 13-2: Writing Recursive Code
Watch on YouTube →
Overview
Bill Moseley demonstrates recursion in Python by building a countdown function that prints its argument, checks whether it is greater than zero, and calls itself with one less. He uses the debugger and a “waking up” print statement to show how recursive calls unwind, then develops a count-up function and explains why every recursive function needs a stopping condition to avoid Python’s roughly 1,000-call recursion limit.
Key takeaways
- A recursive countdown can reduce its argument by one on each call and stop when intStart is no longer greater than zero.
- The base case prevents further recursive calls; once reached, Python returns through earlier calls in reverse order.
- A recursive count-up function can pass both the current value and the endpoint, incrementing the current value until it reaches the endpoint.
- Python’s recursion limit is roughly 1,000 nested calls, so missing or incorrect stopping conditions eventually cause an error.
- For a simple numerical countdown, a loop is more direct, while recursion demonstrates how a function can call itself and unwind.
Chapters
- Bill Moseley contrasts a loop-based countdown using range with a recursive countdown function.
- The recursive countdown prints intStart, then calls countdown(intStart - 1) whenever intStart is greater than zero.
- Starting countdown at 5 produces the sequence 5, 4, 3, 2, 1, 0; Moseley notes recursion is unnecessary for this simple task but useful for learning.
- PyCharm’s debugger shows intStart decreasing from 5 to 0 across successive countdown calls.
- A print statement after the recursive call reveals that execution resumes in reverse order once the condition becomes false.
- Moseley compares the process to descending through nested dreams and then waking up, illustrating how calls return from the deepest case first.
- The countUp exercise uses intCurrent and intEnd, prints the current value, and recurses with intCurrent + 1 while intCurrent is less than intEnd.
- Calling countUp with 0 and 10 prints the integers from 0 through 10.
- Without a condition that stops recursive calls, Python continues descending until its recursion limit, around 1,000 levels, raises an error.
Transcript excerpt
0:00 Let's write some recursive code right now. Why don't you fire up PyCharm? I'm going to give you a minute to get it going before I write any code, but go ahead and fire it up. And we're going to do a simple recursive function together. Yes. Yep. Yep. And you'll see exactly what I mean by that here in just a minute. But basically, the definition of a function contains a call to that function. So that it actually calls itself, a dream inside a dream inside a dream.
0:57 All right. So I'll start nice and slow here with this. But we are going to create a function that will count down from any number we give it to zero and stop. While you do, while you get Python ready to go, I'm going to show you how this would work in a non-recursive function. Right? I would say for int, range, I might say 10 comma 0 comma 1, and I would print int. So if I run this, there we go, counts from zero to, or 10 to one. Now, if I wanted to make that inside a function, I might
1:58 say def countdown, and I would say intStart for the starting integer. And then all I do is take that number and put it here. intStart. And maybe I go to negative one. So it'll stop at zero. Right? So now if I want to call that function, I would say countdown 100, and it's going to count down from 100 all the way to zero. Now that is a non-recursive example. And I will be honest with you, the recursive version of this isn't really a timesaver. This isn't, this isn't, counting down from a number to zero isn't really a problem that I
2:49 would use recursion for typically, but let's do it anyway. So, I'm going to remove the, in the parts
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.