Recursion
Deprecated Lab — Trinket is shutting down
This lab was written for Trinket.io, which is shutting down in August 2026. The embedded trinket.io links on this page will stop working after that date.
These pages are kept for reference only. The current version of this course now runs every lab as an inline Skulpt editor right in the page — no account or install needed. Start at Chapter 1: Welcome to Python.
Recursion¶
Recursion is when we write a function that calls itself. It usually passes a parameter that changes and when it hits a limit it stops going. This lab does a lot of drawing so to speed it up we will disable the turtle motion.
Our recursive function is the draw_branch function. It will draw a single branch and then call itself two times at its tip to draw two more smaller branches.
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
Drawing¶

Run Sample Program on Trinket¶
Run Recursion Program on Trinket
Explanation¶
The first three lines will be the same for all our programs. They import the turtle library into our program, create a new turtle object and then assign the turtle a shape icon.
Note that at the start, the turtle is facing to the right. After the last instruction, it is also facing to the right.
Experiments¶
Can you change the distance and angle the turtle moves? What happens when you change the numbers for the forward and right functions? Can you go left as well as right?
Can you add more motion commands using copy and paste?