In this introductory activity, use turtle graphics to create a drawing in Python!
Access these instructions by visiting bit.ly/coding-activities and clicking on Python!
Python is a popular multi-purpose programming language used by professional and hobbyist developers alike. You can use it to develop games, build websites, control robots, and more!
Trinket is a free online tool that lets you write and run Python code right from a web browser!

from turtle import *
import, and add the following code:
shelly = Turtle()
shelly is now a Turtle object - but Shelly needs a shape! Under that line, add the following code to give Shelly a shape:
shelly.shape("arrow")
The setup code should look something like this:
from turtle import *
shelly = Turtle()
shelly.shape("arrow")
Make things a little more interesting by updating the colors.
shape was updated - just using color instead paper = shelly.getscreen()
paper variable stores the screen. Change its color using bgcolorThe code for the color should look something like this:
shelly.color("green")
paper = shelly.getscreen()
paper.bgcolor("gold")
One of the most useful turtle abilities is the ability to move across the screen and draw like a pen! Create a blank line, and then add the following command on the next line:
shelly.forward(100)
Click the Run button to see the turtle move across the screen! Specifically, it moves forward 100 pixels in the direction it is currently facing (90 degrees).
It is also possible to turn the turtle. Add the following command on the next line:
shelly.right(90)
This command turns the turtle 90 degrees to the right. Previously the direction of the turtle was 90 degrees (pointing to the right), so after turning 90 degrees to the right, the turtle should face down (180 degrees).
Run the program again to see the turtle move to the right, then turn to face down!
Add the following commands to the file, under the existing commands:
shelly.forward(100)
shelly.right(90)
shelly.forward(100)
shelly.right(90)
Run the program to see what this code does. It should draw part of a square! How does that work? On a piece of paper, or on a whiteboard, try to draw the same square as the turtle:
So, the turtle moves 100 pixels to the right, turns 90 degrees to face down, moves 100 pixels down, turns 90 degrees to face left, moves 100 pixels to the left, and then turns 90 degrees to face up!
Complete the square that Shelly is drawing. The code will be the same as the first part of the square:
shelly.forward(100)
shelly.right(90)
At this point, there appears to be a lot of repeated lines of code. This isn’t too big of a deal, but it could be annoying to change the size of the square, or draw a shape with more sides. Luckily, there is a way to automatically repeat commands: the for loop!
Use a for loop to repeat the code that draws the square.
shelly.forward and shelly.rightfor loop
forx inrange(4):4 timesThe code should look something like this:
for x in range(4):
shelly.forward(100)
shelly.right(90)
Much shorter, and easier to maintain! Now, to change the size of the square, only one number has to be updated.
Another cool thing about for loops is they can repeat any number of times. Three or four times isn’t that cool, but repeating 60 times can be very helpful! Use a for loop to draw another type of shape - a star.
First, get to the proper location on the map (without drawing), and change the speed and color as well.
shelly.penup()shelly.goto(-100, 50)shelly.pendown()shelly.speed(20)shelly.color("purple")The code for this part should look like this:
shelly.penup()
shelly.goto(-100, 50)
shelly.pendown()
shelly.speed(20)
shelly.color("purple")
Now that Shelly is in position, create a new for loop that draws a star shape.
for loop structure to repeat 60 times
forx inrange(60):for line, create a new indented line (add a couple spaces in front)The code should look something like this:
for x in range(60):
shelly.forward(200)
shelly.right(186)
Feel free to update the numbers to see how the star changes!
At the end, hide the turtle so that the star can be viewed with nothing in the way!
shelly.hideturtle()
At the end of the code-along, the code should look something like this:
from turtle import *
shelly = Turtle()
shelly.shape("turtle")
shelly.color("green")
paper = shelly.getscreen()
paper.bgcolor("gold")
for x in range(4):
shelly.forward(100)
shelly.right(90)
shelly.penup()
shelly.goto(-100, 50)
shelly.pendown()
shelly.speed(20)
shelly.color("purple")
for x in range(60):
shelly.forward(200)
shelly.right(186)
shelly.hideturtle()
After completing the code-along, take a look at the examples or try out some challenges!