In this article we will cover :
- What Turtle is and how to write basic commands
- What loops are and why we use them
- What variables are and why we use them
- How we can use loops to change parameters such as colour and width
What is Turtle?
TODO potted history of LOGO goes here Insert photo of actual robot
Turtle is a Python module for teaching students the relation between code and action. Writing a command to go forwards, makes the turtle on the screen go forwards! The aim of this course is to relate essential components of programming to a visual representation. In this way students can, hopefully, gain an understanding on the material.
How to write basic commands
In turtle we must important the module. In this course we use :
import turtle as tl
commands are then written suffixed the tl with a dot like so
import turtle as tl tl.forward(100) # move forwards 100 units tl.left(90) # turn left 90 degrees tl.backward(50) # move backwards 50 units
In this way we can have the Turtle draw patterns of our choosing.
What is a loop and why is it important?
A loop is a way of repeating code. This is done so we do not write excessively long programs. It also makes our code much easier to read!
Consider an example where we draw a square
import turtle as tl for _ in range(4): tl.forward(100) tl.right(90)
This is much easier to read and debug than just expanding this over far too many lines.
What is a variable and why is it important?
A variable is essentially a labelled box.
The label, chosen by you, tells the programmer what is contained within.
We define variables using the
Importantly we can alter variables.
This allows us to produce more complex patterns.
Consider how we make a spiral
import turtle as tl side = 50 edges_to_draw = 12 for _ in range(edges_to_draw): tl.forward(side) tl.left(90) side = side + 10
Using loops on lists
The final part of this section looks at using lists with loops. Lists are collections of values. Consider the following example
import turtle as tl tl.width(10) # set the pen width! for colour in ('red', 'green', 'blue', 'yellow'): tl.pencolor(colour) # note the spelling! tl.forward(100) tl.left(90)
The key difference from the original square drawn above is that each side of the square is now a different colour.
Write the code to produce the following videos. Ensure that you use loops where appropriate!
In this section we have covered a brief introduction to programming using Turtle. Important concepts imparted are variables and loops.