Variables, constants and assignment

Storing, changing and naming values, and constants that never change.

F1.6Programming basicsGCSE15 min

Do this lesson in the simulator

A variable is a name that holds a value. It is the first idea in this module that is genuinely new, and every later idea is built on it. Take it slowly.

Giving a value a name

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

speed = 60
print(speed)

Run this in the simulator

speed = 60 stores the number 60 under the name speed. This is called assignment. From then on, writing speed means 60. The = is not "equals" as in maths; it means "put this value into this name". Read it right to left: take 60, store it in speed.

Using a variable

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

speed = 60
print("driving at", speed)
forward(speed, distance=20)
print("done at", position())

Run this in the simulator

Everywhere speed appears, Python uses the value stored in it. Now change the first line to speed = 30 and run again. One change, and both the message and the drive changed. That is the point of a variable: one place to change something that is used in many places.

Naming

A name can use letters, numbers and underscores, and cannot start with a number or contain spaces. Choose names that say what the value is:

speed = 60           # good
leg_length = 25      # good
x = 60               # fine for maths, bad for a speed
s = 60               # nobody knows what s is next week

Names are case-sensitive: speed and Speed are different variables.

Variables can hold the robot's answers

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

before = distance()
forward(50, distance=20)
after = distance()
print("before:", before, "after:", after)
print("the wall got", before - after, "cm closer")

Run this in the simulator

Without variables you could not compare two readings taken at different times. The first reading would be gone by the time you took the second.

Changing a variable

A variable can be given a new value at any time. That is why it is called a variable: it can vary.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

count = 0
print(count)
count = count + 1
print(count)
count = count + 1
print(count)

Run this in the simulator

count = count + 1 looks odd if you read it as maths. Read it as Python does: work out count + 1 using the current value, then store the result back in count. This line, in some form, is in almost every program in the world.

Constants: values that should never change

Some values are fixed for the whole program: the top speed you allow in a classroom, the size of the mat. A value like that is a constant. Python has no special word for it; instead, programmers write the name in capitals as a promise not to change it:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

MAX_SPEED = 40       # the fastest we allow near the desks
LEG = 20             # every side of the drive, in cm

forward(MAX_SPEED, distance=LEG)
right(MAX_SPEED, distance=LEG)
backward(MAX_SPEED, distance=LEG)
left(MAX_SPEED, distance=LEG)
print("back where I started, never faster than", MAX_SPEED)

Run this in the simulator

Four drives use the same two values. To let the robot go faster, you change one line, and you cannot miss one of the four by accident. The capitals tell any reader: this is set once, at the top, and not changed.

Task: speed report

Store 70 in a variable called speed. Print speed: 70 and half: 35, both using the variable (the numbers 70 and 35 must not appear anywhere except the first line). Then drive 20 cm at that speed.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

speed = 70
print("speed:", speed)

Task: three legs

The robot starts at the bottom left and the green square is at the top right. Store three leg lengths in three variables, drive forward, then right, then forward, and print total: <number> using the variables.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

leg1 = 30
# leg2 and leg3 ...

Challenges

  1. Store the battery in a variable at the start, drive around a bit, and print how much battery the drive used.
  2. Add a constant NOTE = 440 to a program and use it for three short beeps with tone.
  3. What happens if you use a variable before giving it a value? Try it and read the error. Which of the three kinds of error is it?