Here you will learn to
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.
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.
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.
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') ?
We make the turtle go backward 100 steps.
from turtle import *
shape('turtle')
back(100)
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.
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.
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.
-- Output from your program will be here --
Question: What will the program draw?
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.
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.
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.
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.
-- Output from your program will be here --
Login to save your progress and your code.
Continue the code and draw a triangle. Don't forget to click "Mark complete" when you are done.
-- Output from your program will be here --