This assignment tests your understanding of variables, and their uses. Variables are probably the most important thing in programming. They store the data that your program is using, and without them, it would be very difficult (if not impossible) to write a useful program. If you missed the first assignment, click here for instructions on how to install Python. If you are unable to install Python then you can use to run your programs on-line.

Variables in Python

All programming languages have variables, and lots of them, including Python, are typed, which means that different types of data - e.g. text and numbers - can be stored and processed appropriately.

Python has four main types of variable - int (whole numbers), float (floating-point/decimal numbers), str (string, i.e. text) and bool (true or false). You can determine the type of a variable in Python by using the type() command - e.g. if you set a = 1, then type(a) will return int. This can be useful as Python can change the type of a variable while your program is running - that is called casting.

There aren't many restrictions on the names of variables - they can have any combination of letters and numbers, but they cannot start with a number or a character that isn't a letter, and they can't be the same as a Python command, e.g. print. It does make sense for the variable name to suggest what it stores, e.g. name for a name or age for an age.


You have to give a variable a value before it is used. Giving a variable a value is know as assigning a value. It's very easy to do, and you can use a value or string, or the result of a calculation. All of the following are valid assignments:

 age = 10
 age = 8 + 2
 name = "Joe"
 fullname = "Joe " + "Bloggs"
 fullname = forename + " " + surname

You can also use the same variable as part of the assignment - this is useful when adding to, or taking away from, a value:

age = age + 1
fullname = "Mr " + fullname

This might look confusing at first, but remember that the = sign means to make it equal, not that it is equal, as in Maths. The variable to the left of the = sign is the new value, and the one on the right is the old value, so age = age + 1 means "make the new value of age equal to the old value plus one".

The following assignments are NOT valid and will result in a type mismatch error because you're trying to mix strings and numbers:

age = "Hello" + 10
name = 10
name = name + "Bloggs"

Related Links

If you want to read a lot more about variables and variable types, have a look at Variables at Computing and ICT in a Nutshell.


Reading programs:

1. In teach of the following examples, select the type of variable that a becomes. (6 marks)
  a = 123
  a = True
  a = "Hello"
  a = 12.3
  a = 3/2
  a = 3.0/2
2. What would be the output of the following program (i.e. what would it print on the screen)?
		message = "Hello World"
(1 mark)
3. What would be the output of the following program?
		a = 10
		b = 2
		print(a + b)
(1 mark)
4. What would be the output of the following program?
		a = "10"
		b = "2"
		print(a + b)
(1 mark)
5. When this program has finished running, what will be the values of a and b?
		a = 10
		b = 5
		a = a + b
		b = b + 1
(2 marks)

Values: a: and b:

6. Some programming languages allow us to have named values that don't change throughout the program. What are these called? (1 mark)


7. The scope of a variable depends on where it is set - what do you call a variable that is created: (2 marks)
  in the main part of a program?
inside a function?
8. When this program has finished running, what will be the values of a and b?
		a = 10
		b = 5
		a = a + b
		b = a - b
		a = a - b
This is a clever programmers' trick, so be a bit careful - you might want to use a pencil and paper to record the values a line at a time.
(3 mark)

Values: a: and b:

What has this program done?

Your name:     E-mail address: