Python is a programming language that allows us to create and build programs.
Python turtle is a library that allows us to create and draw shapes and designs within the Python programming language.
Let's take a look at some Python Turtle code and discuss what each line does.
import turtle
turtle = turtle.Turtle()
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
This python turtle code draws a square. Lets look at what each line does.
This line imports the turtle library so that our python program can use it:
import turtle
This line creates a variable called turtle that is going to store all of the turtles commands from the turtle library. The line is written in the format:
variable = library.commands
turtle = turtle.Turtle()
This line tells the turtle to move forward by 100 pixels (small squares in the screen).
turtle.forward(100)
This line tells the turtle to turn right 90 degrees to face the correct way for the new side.
turtle.right(90)
All the remaining lines are repeats of the last two lines.
We can change the shape of the turtle from the starter triangle into the shape of a turtle by writing this line:
turtle.shape("turtle")
We can also change the colour of the turtle by writing this line:
turtle.color("blue")
Notice the spelling of colour is without the u, this is because Python is written in American English.
When drawing shapes other than a square, we need to change the angle we turn the turtle.
To calculate this we have to calculate the exterior angle as python needs to turn outside the shape. to do this we use the formula:
360 / n
where n is the number of sides the shape has.
for example, if we were drawing a pentagon (5 sides), we would need to do:
360 / 5 = 72
so our python turtle code would be:
import turtle
turtle = turtle.Turtle()
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
A set of events, actions, numbers, etc. which have a particular order and lead to a particular result.
In Turtle, sequence is really important because if you put the instructions in the wrong order, you wont get the shape you were hoping for or you might change colour before you wanted to because you told it to do it too early.
For example:
import turtle
turtle = turtle.Turtle()
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
import turtle
turtle = turtle.Turtle()
turtle.forward(100)
turtle.left(72)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
May look very similar but they have very different outcomes:
Repeating until an amount or condition is met.
Iteration is very powerful in turtle because when drawing shapes, there are lots of repeated lines, both the square and the pentagon we have explored above use forwards and turns in a pattern to draw their shape. This can be shortened with iteration.
We are going to look at a "for loop". This loop says "for every side in the range of the amount of sides the shape has, repeat this pattern".
For example:
import turtle
turtle = turtle.Turtle()
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
turtle.forward(100)
turtle.left(72)
import turtle
turtle = turtle.Turtle()
sides = 5
for side in range(sides):
turtle.forward(100)
turtle.left(72)