Create shapes and make pictures using Python Turtle. Learn how to use variables to store values and loops to repeat code. This is a great way to start learning how to code with Python and to understand some basic coding ideas.
Digital pictures out of code like this one:
By making shapes with code you will learn how to:
Draw lines and make turns with Python Turtle
Use loops to repeat instructions and create shapes
Use variables to alter shapes and colours
Use loops to create patterns
A computer or tablet capable of accessing the online interactive Python environment called Trinket. You can create a trinket.io account using your google school account; or
If you don’t want to create an account you can use the embedded interactive trinkets below to run code and see what happens; or
You can use a computer that has the Python programme installed.
Sign in to your trinket.io account, click on your name and then select ‘New Trinket’ and ‘Python’ from the drop-down menus.
To begin using Turtle in Python, you need to import the Turtle library (a pre-made list of coded commands). Type import turtle. Notice the word ‘import’ appears in purple. This is to show that it is a keyword (a word used in the Python programming language that can’t be used for anything else).
It would be nice if our turtle looked the part, not just a boring cursor, so leave a line and then type turtle.shape("turtle"). Notice that the text inside "" appears in red. This is to show that it is a string (a collection of characters i.e. letters, numbers, punctuation and spaces).
Now you can tell your turtle what to do, for example, to move forward 100 pixels. Leave a line and type turtle.forward(100). Notice 100 appears in blue. This is to show that it is a parameter (a value) for a function (a chunk of pre-programmed code that tells Python to do something). Click on 'Run' or the play icon to run your first Turtle program. What happens? Imagine your turtle is holding an imaginary pen in their mouth!
How do you think you might make your turtle move backwards?
Calling our turtle ‘turtle’ is a bit boring so it’s time to give your turtle a name! To do this you can use a variable (a name/label for something used to store values). I’m calling mine ‘t’ as it requires less typing but you can name yours whatever you like. Under ‘import turtle’ insert a new line and type t = turtle.Turtle(). Then change the commands that follow to use your new name instead of ‘turtle’.
Your turtle is only moving in one direction so far. This is good news if you want to draw a straight line, but to draw a shape such as a square, your turtle is going to have to turn. The Turtle programme uses angles in degrees. There are 360 degrees in a circle so how many degrees are in a right-angled turn?
Add a new line at the bottom of the code and type t.left(90). What happens? What happens if you substitute ‘left’ for ‘right’? What is the parameter in this line of text?
You are on your way to creating a square! What do you need to add to your code in order to complete the square?
To create a square, you have repeated some lines of code. This is not the most efficient way of doing it. Instead of typing out many lines of code, it’s easier to use a loop (a function that repeats a block of code a specified number of times). How many times do you repeat the forward and left commands to get a square?
Try replacing all the forward and left commands you have with the following loop code:
for i in range(4):
t.forward(100)
t.left(90)
Notice that ‘for’ and ‘in’ are keywords and we have chosen the parameter ‘4’ for the range function. How might we change our code to create a cross instead of a square?
What values do we need to change to draw a triangle?
Wouldn’t it be nice to draw a shape just by choosing the number of sides? We can do this by creating a variable. Underneath the line of code that gives your turtle a name, insert a new line and type sides = 5.
Now change the parameter in the ‘range’ function to the new variable range(sides). Can you guess one more parameter that we need to change?
What happens when you choose a large number for the variable ‘sides’? How can we change the parameter for the ‘forward’ function to take account of the variable ‘sides’?
We can also use loops to repeat the shapes we are drawing to make patterns. You can put loops inside of other loops (inner and outer) these are called nested loops. In your code, above your current loop, insert a new line and type for i in range(6): don’t forget your colon or Python won’t know it’s a loop.
Go down to your next line of code and either press the Tab button on your keyboard or press the space bar twice. This indents your code and shows Python that it is within the previous loop. Do the same for your next two lines of code.
Now go to the bottom of your code and with one indent type t.left(360/6). Run your code and see what happens!
Create a new variable called ‘shapes’. Under your code for the variable ‘sides’, insert a new line and type shapes = 4.
Now adjust the code in your outer loop to range(shapes) and t.left(360/shapes). What happens when you change the values for ‘shapes’ and ‘sides’?
Do you think the turtle is a bit slow to draw your shapes? We can speed him up! Under your code t.shape, insert a new line and type t.speed(10). The range of speed is from 1-10 but you can also try t.speed(0) which makes the speed as fast as the computer can handle.
How about changing the width of the line the turtle draws? Insert a new line under t.speed and type t.width(10). You can also try t.pensize(10) which should be the same. The width parameter is measured in pixels. What happens if you try 100?
So far the turtle has been drawing black lines on a white background. Now it’s time to add colour! To set the colour of the turtle pen, insert a new line under t.width and type t.color("blue"). Note we have to use the American spelling of the word colour without a ‘u’. You can also try t.pencolor("blue") which should be the same.
Next you might like to change the colour of the background window. Insert a new line under t.color and type turtle.Screen().bgcolor("cyan").
There are lots of colours to choose from, have a look at this website for a complete list:
wiki.tcl-lang.org/page/Color+Names%2C+running%2C+all+screens
Would you like each shape to be a different colour? Insert a new line under the code to create the variable ‘shapes’ and create a new variable called ‘colours’ to store a list of colours. Type:
colours = ["red", "blue", "yellow", "green", "orange", "purple", "cyan"]
Notice we use square brackets for the contents of a list and each value is separated by a comma. Each item in a list also has an index position. The first is 0, then 1,2,3 etc. so code written as colours = [0] would set the colour to red.
Change the background colour to "black" and delete the line of text for t.color.
Insert a new line after your first loop, indent (so the new code is within the outer loop) and type t.color(colours[i%7]). The loop will now rotate through the list of colours (of which there are 7) as the value of i in the loop changes. % is known as the modulo operator and gives the remainder following a division calculation. So if i is 2: 2/7=0 remainder 2 and the colour selected will be ‘yellow’ (remember the index starts at 0). If i is 20: 20/7=2 remainder 6 and the colour selected will be ‘cyan’.
Python Turtle has penup() and pendown() functions so you can move the turtle without drawing a line. At the end of your code delete the final line of code and type the following:
t.penup()
t.forward(100/sides + 200/shapes)
t.right(360/shapes)
t.forward(200/shapes - 100/sides)
This moves your turtle into its new position before drawing the next shape. Now insert a new line at the start of your inner loop and type t.pendown().
You can hide the turtle at the end of your programme so it doesn’t spoil your picture. Type t.hideturtle() at the end of your code.
Turtle can also draw dots and stamp an imprint of itself. Try the commands t.dot(20) where the parameter 20 is the diameter of the dot in pixels, and t.stamp().
You may have noticed that if you pick the number of sides to be 20 or more you get very close to drawing a circle. If you would like to draw a circle you can use the command t.circle(50) where the parameter 50 is the radius of the circle in pixels. As you are not drawing a polygon you do not need the variable ‘sides’ or the inner loop anymore.
Experiment with different numbers of sides, shapes, pen widths, colours and sizes until you get a pattern you really like!