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.
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.
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.
Create the variables side and angle and give them appropriate values so that the turtle draws a pentagon.
-- Output from your program will be here --
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.
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.
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?
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.
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.
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.
Vary the values of the variables until you get a spiral figure you like. Try to understand how different values change the image.
-- Output from your program will be here --
Login to save your progress and your code.
Write your own code and use repetitions to create a figure of your choice.
-- Output from your program will be here --