Log in to save your progress.
Home
Next >>
OBS! Fönstret är för smalt för att använda pythonlabbet.se. Använd en enhet med tangentbord!

Reference

Click on a function to read more.
Turtle
Function
Short description
back()
Moves the turtle backward
begin_fill()
Starts filling with color
circle()
Draws a circle with optional radius
color()
Determines the color the turtle draws with
end_fill()
Ends filling with color
forward()
Moves the turtle forward
goto()
Moves the turtle to a given coordinate
hideturtle()
Hides the turtle
left()
Turns the turtle left a number of degrees
pendown()
Puts the pen down on the drawing area.
penup()
Lifts the pen from the drawing area
right()
Turns the turtle right a number of degrees
shape()
Changes the drawing shape
Screen().setup()
Creates a drawing area with a given size
speed()
Sets the turtle's drawing speed

Introduction to Turtle

Here you will learn to

How it works

In all sections, there are a number of tasks. At the bottom of the page, it shows how many tasks have been completed and how many there are in the section.

All examples have a green background color. Always try to run the code! Tasks have different background colors depending on the type of task. Press "Mark complete" when you feel done with a task.

There is no requirement that all tasks must be completed to continue.

Take your time! Read the text and examples carefully. Programming is not always easy at first!

Read about why it can be good to learn about programming.

A Turtle

In Python, there is a module called turtle for drawing on the screen. Turtle is easy to use and is primarily used for educational purposes.

You control a turtle that draws with a pen as it walks. The turtle can go forward, backward, and it can turn. It can also lift the pen so that it doesn't draw while it walks and later put the pen down again.

To get access to the drawing functions, we write from turtle import *. That code means that we import all the functions that are in the turtle module. After the import, we can use the functions.

Go forward

We start by making the turtle go forward. The turtle starts in the middle of the drawing area and its head is pointing to the right. Write forward(100) for the turtle to move 100 steps forward. By default, the drawing area is 400 x 400. In a later section, we will learn how to change the size of the drawing area.

In this section, shape('turtle') is used to draw as a turtle. The default shape is a triangle.

Example

We make the turtle go forward 100 steps.

from turtle import *
shape('turtle')

forward(100)

Feel free to try changing the number of steps in the example above. Also see what happens if you remove the line shape('turtle') ?

Example

We make the turtle go backward 100 steps.

from turtle import *
shape('turtle')

back(100)

Turning

To turn the turtle 90 degrees left and right, you write left(90) and right(90) respectively. Inside the parenthesis, you can write any degree number.

Example

Turn left and right by 90 degrees.

left(90) #turn left
forward(100) #go forward
right(90) #turn right
forward(100) #go forward

In the example above, there are so-called comments after the four commands. A comment starts with # and is ignored by Python. Now for the first task.

Login or create an account to save your progress and your code.

What does the code do?

Read the code below and try to figure out what the program draws. Run the program after you answer and see if you were correct.

Question: What will the program draw?

A straight line
A triangle
A square
A circle

Colors

We might want to draw nicer figures with colors. With the function color(color) where color is a so-called text string or just string we can change the color of the turtle's pen. It is fine to call this function several times in a program.

A text string is simply a string of characters, for example letters. In Python, a string is written by using '' or "". We have actually already used a string above when we wrote 'turtle'.

For example, we can call the function color with 'red' or 'blue' as argument. The argument is what is inside the parenthesis, i.e. color. Let's look at an example where we draw with both red and blue color.

Example

Draw a red line and a blue line.

color('blue') #change color to blue
forward(50)
color('red') #change color to red
forward(50)
  

There are 140 different colors with names - here you can see them all. The boxes below show some common colors and their corresponding English names. It does not matter if you use uppercase or lowercase letters for the color.

Black
Silver
Gray
White
Red
Green
Blue
Yellow
Purple
Pink
Orange
Brown
Cyan
DarkBlue
LightBlue
Lime
Magenta
Maroon
Olive
Aquamarine

There are of course ways to draw any color. We will get back to how.

Now you will do the first interactive exercise. Follow the instructions and click "Mark complete" when you are satisfied with what you have done.

Login to save your progress and your code.

Change the code

In the square that is drawn, two sides have a different color than black. Change the code so that the square has different colors on all sides. Don't forget to click "Mark complete" when you are done.

Login to save your progress and your code.

Create

Continue the code and draw a triangle. Don't forget to click "Mark complete" when you are done.

Status
You have finished all tasks in this section!
🎉