Creating Variables

What is a Variable?

A variable represents a value that can change in a program. The two parts of a variable are the variable name and the variable value.

biscuitsFor example 20 biscuits in a biscuit jar could be represented as a variable where the name is biscuits and the value is 20.

A variable must only be one word long, start with a letter and you must declare its value before it can be used. For example:


Remember to use the print command to display something to the screen. Also, you need to print the variable name (biscuits), not the value (20) as the value can change.
Important – When writing variable names we do not need speech marks! (e.g. biscuits, not “biscuits”)

We use variables because the value of something might change as the program is executed. For example, if someone munches a biscuit then the value of our variable changes:


When you run the program (press F5) it will look like this:


Practice Task 1

Variables with Strings


PokeBallIn the example above pokemon is a variable that represents the value “Pikachu”. In programming a collection of alphanumeric characters (letters, numbers and punctuation) is called a string. “Pikachu” is a string. To create a string we use speech marks. Numbers by themselves and variable names do not use speech marks.

Pikachu Box                     Squirtle Box

Some students like to think of a variable like a ‘box’ with a name on the side. For example the box here is called pokemon (variable name) and inside we have Pikachu (variable value). But if we take out Pikachu we can put Squirtle inside instead. A variable can only have one value at a time but it can change throughout the program:


This would output:


Practice Task 2


Variables not working? Tick off these common errors:

Consistent variable name?
Variable name only one word?
Don't use speech marks to print the variable
Does the variable have a value

< Back to Section One