Variables, constants and assignment

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

F1.6Programming basicsGCSE15 min

Covers OCR J277 2.2.1 Programming fundamentals · AQA 8525 3.2.2 Programming concepts · Edexcel 1CP2 6.3.2 Variables and constants

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.

speed = 60 takes the value 60 and stores it in a box named speedspeed = 60take 60, store it in speedafter this linespeed60from now on,speed means 60
Assignment, read right to left: the value on the right goes into the box named on the left. After that line, writing speed anywhere means 60.

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.

The count box after each line: 0, then 1, then 2; each count = count + 1 reads the old value, adds 1 and stores the result backafter count = 0count0print shows 0after count = count + 1count1print shows 10 + 1= 1after count = count + 1count2print shows 21 + 1= 2the same box each time: the old value is replaced, not kept
One box, three moments. Each count = count + 1 works out the right-hand side from what is in the box now, then puts the answer back in the same box, so the old value is gone.

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.

The four drives at MAX_SPEED trace a square of side LEG; with LEG changed to 30 the whole square growsforwardLEGright LEGbackwardLEGleft LEGstart and endMAX_SPEED = 40for all four drivesLEG = 20LEG = 30one line changed,all four sides grow
The four drives use the same two constants, so the robot traces a 20 cm square at a speed of 40. Change LEG to 30 on its one line and all four sides change together.

Task: speed report

Store 70 in a variable called speed. Print speed: 70 and half: 35, both using the variable (the number 70 appears only on the first line, and 35 nowhere). Dividing with / prints 35.0, which is not 35: // divides and keeps a whole number, and lesson F1.9 explains the difference. 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?

Past paper questions on this

Real exam questions that test this, each worked through with a program you can run.