Python (Fall 2026) Module 13-3: Recursion in Practice
Watch on YouTube →
Overview
Bill Moseley connects recursion to self-similar fractals, including the Mandelbrot set, Sierpiński triangle, and a Python turtle dragon curve, then explains its practical use for traversing nested lists. The central technique is to process each list item by counting single items and recursively calling the same function for sublists, allowing programs to handle structures whose depth is unknown.
Key takeaways
- Recursion is useful when a problem contains smaller instances of the same problem and its depth cannot be known in advance.
- Fractals such as the Sierpiński triangle demonstrate self-similarity, where a structure contains smaller versions of the same structure.
- The Python turtle dragon curve is drawn as one continuous line that does not cross itself, and its color changes gradually.
- A nested list can be treated as a tree whose branches may extend to an unknown depth, making recursive traversal a natural approach.
- A recursive list processor distinguishes leaf items from sublists: count leaf items directly and call the same function on each sublist.
Chapters
- The Mandelbrot set is drawn as plotted dots in a specific pattern, with additional levels continuing at smaller scales.
- The Sierpiński triangle illustrates self-similarity: triangles are formed from smaller triangles.
- A Python turtle draws the dragon curve as a gradually color-changing, continuous line that never crosses itself; its code is available in the sandbox.
- Recursion suits problems that break into smaller versions of themselves when the number of levels is unknown.
- Nested lists are also called tree structures because branches can contain further lists, potentially extending to thousands of levels.
- The example task is to count all items in a nested list, which may contain tens or hundreds of thousands of elements.
- The traversal processes each item in a list: count it if it is a single item, or recursively process it if it is another list.
- Recursive calls move into sublists and return to the parent list when each sublist is finished, preserving the traversal order.
- The exercise begins a Python function named processList and asks students to write the traversal steps as comments.
Transcript excerpt
0:00 Now, recursion is used for all kinds of interesting things. Here are some other recursive fractals that are kind of interesting to see. This is called a Mandelbrot set. I know it's kind of dim here, but it was drawn using the turtle. So, it's plotting these dots in a specific pattern. And if this continues, it would actually go to the next level of size. Another interesting one is a Sierpinski triangle. You don't have to remember these. Won't be on the test, cuz there is no test. But, this is a triangle made up of triangles made up of triangles made up of triangles. What's that?
0:55 It, it is not the Triforce. I'm not teaching class on The Legend of Zelda. I kind of wish I could, but no. But maybe that was the inspiration for the Triforce. I don't know. But this is, this is the Sierpinski triangle. And you'll notice it, a lot of these fractal designs are something made up of something made up of something, and, and the something is the same. So, you're seeing that pattern of smaller versions of the same thing being presented over and over. Now, this happens to be one of my favorite fractal designs. It's called the dragon curve, and it is a simpler version of that first fractal that I showed you, but done using the Python, done using the Python's turtle. Man, I'm just going to make it dark for 1 second so
1:51 you can see this a little bit better, cuz it really is kind of a cool design. There we go. So, and I like this particular version because I've told it to gradually change color. But, you can start to see, they, the, even though it's sort of a low-resolution version of itself,
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.