Log in to save your progress.
<< Previous
Home
Next >>
Note! The window is too narrow to use Pythonlab.dev. Use a device with a keyboard!

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

Variables and repetitions

Here you will learn to

Now we will learn about two of the most basic concepts in programming. First variables, which are used to save values in memory. Second repetitions, which are used to repeat the same piece of code a number of times.

Variables

A variable is created by writing for example side = 50. The name of the variable here is side and the value 50 has been saved in it. After the variable is created, we only need to write the name of the variable to access the value in it.

It is called a variable because the value can vary during the execution of the program. For example, we can assign a new value side = 100 later in the program.

In this turtle course, the values of variables will be numbers. In general, variables can contain very different types of data, see the basic course on variables.

Example

Draws an equilateral triangle with a side length defined by a variable. Try changing the value of the variable.

side = 50

forward(side)
left(120)
forward(side)
left(120)
forward(side)
left(120)
      

In the example above, we could of course have written angle = 120 and used that variable with left(angle). In this particular case, however, there is not much point in varying the value of the angle since only 120 degrees gives a triangle.

Login to save your progress and your code.

Modify the code

Create the variables side and angle and give them appropriate values so that the turtle draws a pentagon.

Repetitions

With a so-called for loop, a piece of code can be repeated a number of times. This is very practical to avoid writing the same code multiple times.

We can write for i in range(4): to repeat code four times. The expression range(4) corresponds to the interval [0,3], which is four different integers. For each repetition, the value of the variable i is updated. In the first repetition, i = 0, then i = 1 and i = 2, to finally be executed a last time when i = 3. A total of four repetitions.

Example

Draws a square by repeating "go forward" and "turn left" four times.

for i in range(4):
    forward(100)
    left(90)      
  

Note that the line with for above ends with a : (colon). It is also important to note that the code to be repeated is indented from the left, so-called indented. In Python, lines are usually indented with four spaces. You can press TAB to automatically write four spaces here at Pythonlab.dev. Read more in the basic course introduction.

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?

20 circles drawn along a circular arc
20 circles drawn along a vertical line
20 circles drawn along a horizontal line

Now we will combine our newfound knowledge of variables and repetitions. By using the fact that the variable i has different values during the repetitions, we can draw interesting things. For example, we can multiply by the value of i to go a longer distance for each repetition.

Example

Draws a pattern by utilizing the fact that the variable i increases by one for each repetition.

for i in range(50):
    forward(5*i)
    left(90)  
  

Do you think the turtle went a little too slow? To let the turtle draw as fast as possible, use speed(0). The argument to speed should be an integer in the interval [0,10]. By default, the speed is set to 3.

Example

Uses speed(0) to draw the pattern in the example above as fast as possible. Feel free to try other values for the speed.

speed(0)
    
for i in range(50):
    forward(5*i)
    left(90)  
  

Now follow two tasks that let you experiment with what you have learned.

Login to save your progress and your code.

Modify the code

Vary the values of the variables until you get a spiral figure you like. Try to understand how different values change the image.

Login to save your progress and your code.

Create

Write your own code and use repetitions to create a figure of your choice.

Status
You have finished all tasks in this section!
🎉