Module
notebook
beginner
Python

Day 1 Homework: Visual Programming With Turtle

If you've taken an Intro to Python module, then this challenge will help you apply what you've learned by using the Turtle program to visualize some of these programming concepts

Sign in to run notebook cells.You can read the code, but running requires an account.
Python 3· Idle

Open In Colab

Python Practice with Turtle

Welcome to your first hands-on Python practice session! In this notebook, we'll use a drawing tool called Turtle to explore programming concepts in a fun, visual way.

What is Turtle?

Turtle graphics has a long history in computer science education. The original Turtle was part of the Logo programming language, created in the 1960s to help children learn to code. The idea is simple: you control a small "turtle" on the screen by giving it commands like "move forward" or "turn right," and it draws lines as it moves.

Python includes a built-in turtle module that works the same way. However, that module requires a graphical window, which doesn't work inside a browser-based notebook. To solve this, we'll use a library called ColabTurtlePlus, which adapts Python's Turtle to work directly inside Google Colab.

Important: You must open this notebook in Google Colab for the turtle graphics to display. Running it locally will not work.

Setup

We will begin by installing the ColabTurtlePlus library. The first code cell prepares the Turtle tools that this notebook uses. You can learn more about the library in its PyPI project page.

[ ]

Installing and Importing the Library

Now that we've installed ColabTurtlePlus, we need to import it so we can use its functions in our code. The line below imports everything from the library and makes all of its drawing commands available to us.

[ ]

Setting Up Our Display Screen

Before we can start drawing, we need to set up a blank canvas. Think of this like getting a fresh sheet of paper to draw on.

Here's what each line does:

  • clearscreen() — Clears any previous drawings and gives us a fresh start. Each time we want to create a new drawing, we'll call this first.
  • setup(500, 500) — Creates our drawing canvas with a width of 500 pixels and a height of 500 pixels.
  • T = Turtle() — Creates a new turtle object and stores it in a variable called T. This turtle is our artist — it can move around the canvas, turn, and draw lines. We'll give it commands to create our drawings.
[ ]

Customizing Your Turtle

We can change certain attributes about our turtle to make it look different. For example, we can change its shape so it actually looks like a turtle on screen, and we can change its color. Try modifying the values below and re-running the cell to see what happens!

[ ]

Making the Turtle Move: Drawing a Square

Once we've created our T object, our turtle has different commands that we can use to make it move around the canvas.

Moving commands:

  • .forward(distance) — Makes the turtle move forward in whatever direction it's currently facing. The number you pass in is how many pixels it will travel.
  • .backward(distance) — Makes the turtle move in the opposite direction from where it's facing.

Turning commands:

  • .right(degrees) — Turns the turtle to the right by the specified number of degrees.
  • .left(degrees) — Turns the turtle to the left by the specified number of degrees.

Let's use these commands to draw a square. A square has four sides of equal length, with 90-degree turns between each side:

[ ]

Adding Color to Our Shapes

Let's make the same square, but this time add some color! We can use T.color(border_color, fill_color) to set both the outline and fill colors. To actually fill the shape, we wrap our drawing commands between T.begin_fill() and T.end_fill().

[ ]

Circles

You can make your turtle draw a circle by calling T.circle(radius), where the number you pass in is the size of the radius (the distance from the center to the edge of the circle).

Below, we're going to draw two circles: one with a yellow fill and blue border, and the other with a blue fill and yellow border. Notice how we change the color settings between the two circles.

[ ]

Penup and Pendown

By default, the turtle draws a line everywhere it moves — like dragging a pen across paper. But sometimes you want to move the turtle without drawing. That's where T.penup() and T.pendown() come in:

  • T.penup() — Lifts the pen off the paper. The turtle can move, but it won't draw anything.
  • T.pendown() — Puts the pen back on the paper. The turtle will draw again as it moves.

This is just like lifting your pen off a piece of paper to move to a new spot without leaving a mark. In the example below, we use a loop to draw a dashed line:

[ ]

Drawing with Loops

Turtle graphics is a great way to see the power of using loops in your code. A loop lets you repeat a set of instructions multiple times without writing them out again and again.

Take a look at the following drawings, and imagine how many lines of code it would take to create them without using loops. Then try playing around with the values (like the number of repetitions or the circle size) and observe the effects on the drawing!

[ ]

Example: Circle the Squares (Nested Loops)

Nested loops — a loop inside another loop — become even more powerful with Turtle. In a nested loop, the inner loop runs completely for each iteration of the outer loop.

Let's go back to drawing squares, but this time, after each square we draw, we'll turn the turtle slightly to the right (by 5 degrees) before drawing the next one. Watch what pattern emerges!

[ ]

Notice how the outside edges of the squares give the appearance of a curve? That's because each square is rotated just a tiny bit from the last one.

Let's take this further — instead of just 10 squares, let's draw enough to go all the way around (a full 360° rotation). We'll also speed up the drawing using T.speed() so we don't have to wait too long:

[ ]

Polygon Practice

While we've been creating our shapes, we've been using an important rule: there are 360 degrees in a full rotation around a point.

This rule helps us draw any regular polygon (a shape where all sides and angles are equal). For any regular polygon, the turtle needs to turn a total of 360° to end up facing its original direction. So the angle of each turn is:

turn angle = 360 ÷ number of sides

For example:

  • A square has 4 sides, so each turn is 360 ÷ 4 = 90°
  • A triangle has 3 sides, so each turn is 360 ÷ 3 = 120°
  • A pentagon has 5 sides, so each turn is 360 ÷ 5 = 72°

Let's see this in action! Below, we draw a square by setting sides = 4:

[ ]

A Triangle

Now let's draw a triangle. We just change sides to 3 — the formula handles the rest:

[ ]

A Pentagon

And here's a pentagon with 5 sides. Notice how the code is almost identical — only the number of sides changes:

[ ]

Exercise

Recreate the following drawing below, featuring 7 lines of different colors. Each line is 75% of the length of the previous line. The lines are spaced 50 pixels apart

[ ]

Exercise: Create Your Own Drawing!

Now it's your turn! Using what you've learned in this notebook, create your own turtle drawing in the cell below.

Here are some ideas to try:

  1. Draw a star — A five-pointed star can be drawn by moving forward and turning 144° five times. (Hint: 144 = 360 ÷ 5 × 2)
  2. Draw a house — Combine a square (for the walls) and a triangle (for the roof).
  3. Create a spiral — Use a loop where the forward distance increases each time.
  4. Make your own pattern — Combine loops, colors, and shapes to create something unique!

Remember the tools you have:

  • T.forward(), T.backward(), T.right(), T.left() for movement
  • T.color(), T.begin_fill(), T.end_fill() for colors
  • T.penup(), T.pendown() for breaks in drawing
  • T.circle() for circles
  • T.speed() to control drawing speed
  • Loops (for _ in range(n):) to repeat actions

Summary and Next Steps

Congratulations! 🎉 In this notebook, you've learned how to:

  • Import a library and use its functions
  • Create and customize a Turtle object
  • Move your turtle using forward(), backward(), right(), and left()
  • Add color to your drawings with color(), begin_fill(), and end_fill()
  • Draw circles with T.circle()
  • Use penup() and pendown() to control when the turtle draws
  • Use loops to repeat actions and create complex patterns
  • Draw polygons using the 360° rule

These are real programming skills! Variables, loops, functions, and objects are the building blocks of every Python program — we just used them to draw pictures.

Keep experimenting with Turtle on your own. The more you play with it, the more comfortable you'll become with writing Python code. Happy coding! 🐢