Introduction to Python using Turtle : A Review

Posted on 11-06-19 in Computing

This post is part 5 of the Introduction to Programming" series
Introduction to Python - Lesson One

Introduction to programming using Python and Turtle

Introduction to Python - Lesson Two

Continuing our introduction to Python using Turtle

Introduction to Python - Lesson Three

Looking at loops and conditionals in more detail

Introduction to Python - Lesson Four

Further exploration of Turtle and an introduction to colour

Introduction to Python using Turtle : A Review

A brief review of the first part of this Turtle course

Introduction to Python - Using Colour

This is an aside / extension material for students who get ahead

In this article we will cover :

  1. What Turtle is and how to write basic commands
  2. What loops are and why we use them
  3. What variables are and why we use them
  4. 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.

How to draw a square

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 = symbol. 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
Visual representation of the simple spiral written in the code above

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)
A square with each side a different colour

The key difference from the original square drawn above is that each side of the square is now a different colour.

Review Exercises

Write the code to produce the following videos. Ensure that you use loops where appropriate!

embedded video, no alt provided embedded video, no alt provided embedded video, no alt provided embedded video, no alt provided

Summary

In this section we have covered a brief introduction to programming using Turtle. Important concepts imparted are variables and loops.