Turtle

In order to better understand how Python code works we will write instructions for moving a little turtle, and see the effects illustrated live.

Turtle module

The following program will draw a small figure that quickly moves to the right:

You can try the program here or copy the code to your own computer and run in IDLE. 

The first line tells Python that we need to use a modul, a collection of code for a certain usecase that someone else already wrote. One module that comes pre-packaged with Python is called "turtle". We will see more modules later.

The next line decide the shape of the turtle, which is a "turtle" in this case. You can use something else if you want, try "arrow" for example.

The next line decides what color it has. Try changing it to your own favorite color, perhaps 'green', 'blue', 'black', or 'white'.

The last and final line is an instruction for the turtle to move forward 100 steps. Here, it moves 100 pixlar on the screen, to the right. Try changing the number to 50 or 200 to see how it moves differently.  

Turning

We can also tell the turtle to turn a number of degrees to the left or to the right. Try adding the following two lines:

turtle.forward(100)
turtle.left(90)
turtle.forward(100)

This makes the turtle first walk 100 steps to the right, turn 90 degrees to the left (counter-clockwise) and then move 100 steps forward (upward).

A complete program that draws a square looks like this: 

# Import the turtle module and decide color

import turtle
turtle.color('red')
turtle.forward(100)

# Draw part 1 

turtle.forward(100)

# Draw part 2 

turtle.left(90)
turtle.forward(100)

# Draw part 3 

turtle.left(90)
turtle.forward(100)

# Draw part 4 

turtle.left(90)
turtle.forward(100)

Dokumentation, andra instruktioner 

We can also tell the turtle to draw a wider line when it moves with this instruction: 

turtle.width(5)

Remember, the computer reads instructions in order, from top to bottom. So, in order to see any difference, you must put this instruction on a line before a forward instruction. Try adding it before drawing part 3.. 

Do you think the turtle doesn't really look like a turtle? We can change its shape with another instruction. Add this line right after the color has been selected:

turtle.shape('turtle')

You can find all possible turtle instructions here: https://docs.python.org/3/library/turtle.html 

On that page you find what's known as the documentation for the turtle module, a description of everything that can be done. Take a look at the page if you want, but it is quite technical. We will go through more parts of the turtle module together during the coming sections.

Read page in other languages

In this video, we are using the turtle module and slowly, line by line, observe what happens as it draws a square.

  • Modul: We can reuse code that others have written, in our programs. To not get overwhelmed by all the code, we divide it up into modules that can be imported as needed.
  • Pixel:A screen consists of thousands of tiny pixels. Therefore a pixel is the smallest distance that something can be moved.
  • Dokumentation:Description and summary of code.
Difficulty level