CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: Sequences and Functions with Turtle


  1. Sequences of instructions
  2. A simple square in Turtle
  3. Repetition
  4. Functions
  5. Turtle API

  1. Sequences of instructions
    We will start our discussion of Python by talking about algorithms. An Algorithm is a sequence of instructions that in the end solves a specific problem. While specifying the instructions, it is extremely important that we apply the following principles:
    1. We have to make sure we have all the instructions - no missing parts
    2. We have to make sure we have the instructions in the right order
    3. There should not be any unwanted/extra/useless instructions

    Let's understand this with a simple example. Lets say you have two commands:
    • Forward n - Moves forward by n meters and draws a line on the ground
    • Left d - turns left by d degrees

    Our simple task is to use the above commands to draw a square of length 10 meters on the ground. Its not too hard to imagine that the following sequence of instructions will achieve this task:
        Forward 10
        Left 90
        Forward 10
        Left 90
        Forward 10
        Left 90
        Forward 10
        Left 90
            
    You can argue that the last Left 90 is not necessary and you would probably be correct. The above example points out two things: One, that we need to know what are the basic commands are we are able to execute (in this case Forward and Left) and two, we need to put those commands in the right sequence to achieve our final results.

    In this set of notes, we will look at a library in Python called Turtle graphics to practice writing simple algorithms. But before we can get there, let's look at how we can get started with Python.

  2. A simple square in Turtle
    Python comes with a pre-installed library called Turtle. Turtle is a simple graphics library that allows you to draw on the screen using simple primitives. Two of these primitives are: a function called forward(n) and a function called left(d). Similar to our example above, forward(n) moves the turtle in the direction it is facing by n centimeters while drawing a line on the screen as it moves. The function left(d), makes the turtle turn left by d degrees. We can draw a square on the screen by following the given instructions:
    • Start Thonny
    • Go to file menu and click New to start a new Python code editor
    • Type the following code:
      from turtle import * # this line allows us to use the Turtle library forward(100) left(90) forward(100) left(90) forward(100) left(90) forward(100) left(90)
    • Press F5 to save and run the program
    • You should see the turtle move around on the screen and draw a square

    The program should, in the end, produce a picture like the following:

  3. Repetition
    You might have noticed in the above example that we had to repeat a set of instructions (forward(100) and left(90)) four times to draw the square. This repetition is a common occurrence in programming so there is an easy construct for this in the Python language.

    You can use something called a for loop to repeat a sequence of instructions a set number of times. For example, we can write the square program in the following way:
    from turtle import * # this line allows us to use the Turtle library for i in range(4): forward(100) left(90)
    The statements that are indented after the for i in range(4) instruction are executed four times in sequence.
    In a generic case, if we have a sequence of n instructions that we need repeated xx number of times, we can write the following code:
    for i in range(x) statement 1 statement 2 statement 3 ..... statement n

  4. Functions
    Let's take another look at our two different programs capable of drawing a square:
    from turtle import * forward(100) left(90) forward(100) left(90) forward(100) left(90) forward(100) left(90)
    and
    from turtle import * for i in range(4): forward(100) left(90)
    Both pieces of code perform the same task: drawing a square. What if I want to draw lots of squares? It would be useful if I could take a piece of code like this, name it drawSquare, and then be able to use it from other places in my code.

    Lucky for us, we can! I can give a name to a set of instructions (which we will now call a function) by using the def (short for define) command. Consider the following example of defining the drawSquare function:
    def drawSquare(): for i in range(4): forward(100) left(90)
    Note that you need the open and close parenthesis followed by a colon (:) right after the function name.

    The function body (the statements that are part of this function) are indented by one tab. This is called defining a function. Note that when you define a function, it does NOT cause the function to be executed. If we want to run the function, we would call the function as follows:
    drawSquare()
    Let's combine these two things into one program that draws a square using a function:
    from turtle import * def drawSquare(): for i in range(4): forward(100) left(90) drawSquare()
    Now I can take advantage of this new function and use it to draw several squares:
    from turtle import * def drawSquare(): for i in range(4): forward(100) left(90) drawSquare() forward(200) drawSquare() forward(200) drawSquare() forward(200)
    Take some time now to copy that code into Thonny and run it. You should get a picture like the following:

    Then, change some of the values (such as the 100 or the 200) and see what happens.

  5. Turtle API
    Turtle can do much more than what we have demonstrated so far. Consider the following table that summarizes some of the most important things that turtle can do:
    Command Functionality
    from turtle import * Make the turtle module/package available for use
    forward(distance) Moves the turtle forward distance, drawing a line behind the turtle
    backward(distance) Moves the turtle backward distance, drawing a line behind the turtle
    right(angle) Turns the turtle right by angle degrees
    left(angle) Turns the turtle left by angle degrees
    penup() Stop all drawing until pendown is called
    pendown() Resume drawing after a call to penup()
    color(color) Change the turtle's current color
    done() Stop drawing with turtle
    This is not the entirety of Turtle functionality. You can find that in the full documentation of the Turtle API, which can be found in the Turtle API on python.org. You will very likely need to reference it in order to draw more complex shapes or pictures (such as in the homework).