Lesson 2

Variables

If we want a computer to remember something, we have to use a variable. Variables allow us to store and then retrieve data.

We use the single = to assign a number to a variable. Variables can be called pretty much anything, but there can be no spaces in the name. It's a good idea to choose a name that matches the variable's use.

So for this example, I'll call the variable 'test_var'.

Remember that the lines beginning with a # are comments - you don't need to type them in.

1
2
3
4
5
6
7
8
9
#store a number in a variable called 'test_var'
test_var = 10

#retrieve the variable
print(test_var)

#update the variable
test_var = test_var + 1
print(test_var)

Looping

When we want to avoid repeating lots of code, we can use a loop. Here we store the number of times we've already done the loop in a variable called loops.

1
2
3
4
5
loops = 0
while loops < 20:
    print("hello")
    print(loops)
    loops = loops + 1

Turtle documentation

You can look up new turtle functions with the documentation.