Learn To Program With Turtle
Learn To Program With Turtle
Turtle!
By Reuben Mitchell
1
Table of Contents
Getting Started with Python ...........................................................................................................................4
Downloading Python...................................................................................................................................4
Writing Your First Program .........................................................................................................................4
Introduction to Turtle .....................................................................................................................................5
What is Turtle?............................................................................................................................................5
How to use Turtle .......................................................................................................................................5
Making Your First Turtle Program...............................................................................................................6
Code Explanation ........................................................................................................................................6
More Common Commands.........................................................................................................................7
Example Code .................................................................................................................................................9
Example Code 1 ..........................................................................................................................................9
Example Code 2 ........................................................................................................................................10
Example Code 3 ........................................................................................................................................11
Example Code 4 ........................................................................................................................................12
Introduction to Main Content.......................................................................................................................13
Outline ......................................................................................................................................................13
Beginner Code...........................................................................................................................................13
Intermediate Code ....................................................................................................................................13
Advanced Code .........................................................................................................................................13
How to Draw a Square ..................................................................................................................................14
Step by Step: .............................................................................................................................................14
Output:......................................................................................................................................................14
Beginner: ...................................................................................................................................................15
Intermediate: ............................................................................................................................................16
Advanced ..................................................................................................................................................17
How to Draw a Triangle ................................................................................................................................18
Step by Step: .............................................................................................................................................18
Output:......................................................................................................................................................18
Beginner: ...................................................................................................................................................19
Intermediate: ............................................................................................................................................19
Advanced: .................................................................................................................................................20
How to Draw a Pentagon ..............................................................................................................................21
Step by Step ..............................................................................................................................................21
2
Output:......................................................................................................................................................21
Beginner: ...................................................................................................................................................22
Intermediate: ............................................................................................................................................23
Advanced: .................................................................................................................................................23
How to Draw a Hexagon ...............................................................................................................................24
Step by Step ..............................................................................................................................................24
Output:......................................................................................................................................................24
Beginner: ...................................................................................................................................................25
Intermediate: ............................................................................................................................................26
Advanced: .................................................................................................................................................26
How to Draw an Octagon..............................................................................................................................27
Step by Step ..............................................................................................................................................27
Output:......................................................................................................................................................27
Beginner: ...................................................................................................................................................28
Intermediate: ............................................................................................................................................29
Advanced: .................................................................................................................................................29
How to Draw a Rectangle .............................................................................................................................30
Step by Step: .............................................................................................................................................30
Output:......................................................................................................................................................30
Beginner: ...................................................................................................................................................31
Intermediate: ............................................................................................................................................31
Advanced: .................................................................................................................................................32
Challenge ......................................................................................................................................................33
About the Author ..........................................................................................................................................34
Contact ......................................................................................................................................................34
3
Getting Started with Python
Downloading Python
If you are new to Python or programming and want to try writing your first program before learning how
to use Turtle, then this tutorial is perfect for you:
https://www.pitt.edu/~naraehan/python3/getting_started_win_first_try.html.
4
Introduction to Turtle
What is Turtle?
Turtle is a Python programming tool that can be used to draw pictures like this one:
5
Making Your First Turtle Program
Now that you have imported turtle, you can start to use it.
The simplest program you can make is to draw a straight line.
Type the following code into a Python file and run it.
If successful, a new window will open, and you will see the following drawing.
Code Explanation
turtle.forward(100) ➔ Tells Python to draw a line of length 100 (no units).
turtle.done() ➔ Tells Python you have finished your drawing (you should add this to the end of all
of your programs).
6
More Common Commands
Don’t worry if the following table doesn’t make much sense yet. There will be example code later which
aims to make everything clearer.
Command Description
turtle.forward(x) Draw a line in the forward direction of
size x (where x is a number)
turtle.backward(x) Draw a line in the backward direction
of size x (where x is a number)
turtle.right(x) Turn the pointer to the right by x
degrees (where x is a number)
turtle.left(x) Turn the pointer to the left by x
degrees (where x is a number)
turtle.circle(x) Draw a circle with a radius of x (where
x is a number)
turtle.pendown() When the pen is down it will draw
when moving
turtle.penup() When the pen is up it will move
without drawing
turtle.pensize(x) Sets the width of the pen to be x wide.
The larger x is, the thicker the pen will
be (where x is a positive number)
turtle.pencolor(x) Sets the colour of the line when it is
drawing (where x is the name of a
colour)
turtle.fillcolor(x) Sets the colour of the inside of the
shape when drawing (where x is the
name of the colour)
turtle.begin_fill() Tells the turtle when to start filling in
the shape with the specified colour (to
be used just before drawing a shape to
be filled)
turtle.end_fill() Tells the turtle when to stop filling in
the shape with the specified colour (to
be called just after drawing a shape to
be filled)
turtle.showturtle() Show the pointer when drawing
7
turtle.hideturtle() Don’t show the pointer when drawing
8
Example Code
It’s worth noting that any text that is in red is a comment. A comment is used to explain what is
happening in your program. To write a comment, start the line with a hashtag “#”.
Example Code 1
9
Example Code 2
10
Example Code 3
11
Example Code 4
12
Introduction to Main Content
Outline
Throughout the next section of this tutorial, you will learn how to draw a:
1. Square
2. Triangle
3. Pentagon
4. Hexagon
5. Octagon
6. Rectangle
Each shape will be split into 3 sections:
1. Step by Step ➔ This will explain the theory behind the drawing of the shape.
2. Output ➔ This will show you an image of the shape you will learn to draw.
3. Programming ➔ This will include all the code you need to draw the shape. This section will be
split into 3 different levels. Beginner, Intermediate and Advanced.
Beginner Code
The beginner code is a great place to start if you have never programmed before. Once you have
mastered the beginner code you can move onto the intermediate code.
Intermediate Code
The intermediate code introduces the concept of loops. In particular, a type of loop called a “while loop”.
Loops are a fundamental part of programming because they allow us to run the same section of code
repeatedly without the need to write long lists of commands. If you want to learn more about loops, the
following is a great resource: https://www.learnpython.org/en/Loops.
Advanced Code
The advanced code uses slightly more complex loops and introduces the concept of functions. Functions
are also a fundamental part of programming because they allow us re-use sections of our code. If you
want to learn more about functions, the following is a great resource:
https://www.learnpython.org/en/Functions.
13
How to Draw a Square
Step by Step:
Starting from the bottom left corner:
1. Draw a straight line of length 100.
2. Rotate the pointer by 90 degrees.
3. Repeat steps 1-2 three more times.
Output:
14
Beginner:
15
Intermediate:
16
Advanced
17
How to Draw a Triangle
Step by Step:
Starting from the bottom left corner:
1. Draw a line of length 100.
2. Turn the pointer left by 120 degrees.
3. Repeat steps 1-2 two more times.
Output:
18
Beginner:
Intermediate:
19
Advanced:
20
How to Draw a Pentagon
Step by Step
Starting from the bottom left corner:
1. Draw a line of length 100.
2. Turn the pointer left by 72 degrees.
3. Repeat steps 1-2 4 more times.
Output:
21
Beginner:
22
Intermediate:
Advanced:
23
How to Draw a Hexagon
Step by Step
Starting from the bottom left corner:
1. Draw a line of length 100.
2. Turn the pointer left by 60 degrees.
3. Repeat steps 1-2 five more times.
Output:
24
Beginner:
25
Intermediate:
Advanced:
26
How to Draw an Octagon
Step by Step
Starting from the bottom left corner:
1. Draw a line of length 100.
2. Turn the pointer left by 45 degrees.
3. Repeat steps 1-2 7 more times.
Output:
27
Beginner:
28
Intermediate:
Advanced:
29
How to Draw a Rectangle
Step by Step:
Starting from the bottom left corner:
1. Draw a line of length 200.
2. Turn the pointer left by 90 degrees.
3. Draw a line of length 100.
4. Turn the pointer left by 90 degrees.
5. Repeat steps 1-4 one more time.
Rectangles are more complex to draw because not all their sides are the same length. In the intermediate
and advanced example, a new concept is introduced. These are called conditional statements. They are
another fundamental part of programming as they allow us to run different sections of code depending
on the outcome of the statement. If you want to learn more about conditional statements, the following
is a great resource https://www.learnpython.org/en/Conditions.
Output:
30
Beginner:
Intermediate:
31
Advanced:
32
Challenge
Now it’s time for you to get creative.
You have all the tools now to draw something fantastic!
Here is an example of something that I drew using only the code included in this document:
33
About the Author
Hello, my name is Reuben Mitchell. I am currently studying Mechatronic and
Robotic Engineering at the University of Sheffield. I have a passion for
software development, and I am keen to share this with the next
generation.
One of the first programming tools I learnt to use was Turtle, which is why I
have created this tutorial with the aim to provide a gateway into
programming.
Contact
34