1.2.8.0 Variables, Constants and Data Types
1.2.8.0 Variables, Constants and Data Types
Outputs
An output is something that a program produces. It could be displaying words on the screen or playing
music through the speaker.
Python Example:
In python we output text onto the screen using the print() function.
print(“Hello World”)
Inputs
An input is any data that the user enters. They may need to enter their age and address into an online form,
or they may use a gamepad to send directional data to the computer to control a character on the screen.
Once data has been entered, the computer program will need to store that data, even if it is for a short
amount of time. This is done by assigning the data into variables.
Variables
Variables are simply memory locations that can store a single piece of data (of a
particular data type) at any one time. You can visualise a variable as a storage box
which is given a name and contains an item. Variables are given IDs (names) so they
can be accessed in a program. The important thing to recognise is that a variables
contents can change whilst the program is running. Their data can be overwritten.
Programming Variables - Conventions…
All variables’ names in a program need to differ from one another. They have to begin
with an alphabetical character (a-z) and have no spaces or symbols (numbers and underscores are ok!). You
could use camel case: eg: lengthOfCar. This is where words that make up a variable name begin with
capitals (except for the first word).
Variable Assignment
As we already know, variables are used by programs to store data. The operation of placing data into a
variable is known as assignment. In most languages the operator (symbol) used is an equals sign.
Important things to know:
Computer Science UK Membership Site Licence: Do not share outside your centre. 1
A-Level Computer Science
The assignment operator mustn't be confused with the operator to check if a value mathematically equals
another. As the single ‘equals sign’ (=) is reserved for assignment, many languages will have another
operator such as ‘double equals’ (==).
Assignment is always written from right to left. The variable is placed to the left
of the assignment operator and the value to the store is placed on the right.
Python Example:
In python we ask the user for an input and store what they type in, in a variable:
variable = input(“What is your name?”)
Constants
Constants are just like variables in that they are also memory locations. Like variables,
you can think of a constant as storage boxes holding one piece of data (of a particular
data type). Also like variables, constants are given IDs (names). The important thing to
recognise is that unlike a variable, a constant’s contents cannot change whilst the
program is running. They cannot be overwritten.
When might we use constants in programs?
Because constants cannot change, they are great if we want to use a value in our
program that is “set in stone” . Examples are VAT and the value of Pi. Also, they can be useful because if
VAT changed in the future it will be quicker and easier to update the constant (which will be declared once)
instead of every instance of the variable throughout a program.
Data Types
A data type is a formal description of the kind of data used in a computer program. It is vital that we state
what data type a variable is to hold when it is declared for the following reasons:
How an item of data is stored in the computer depends on its data type: The computer will allocate
some memory of the correct size of the variable declared and it can’t do this without knowing what
data type the variable is of.
How an item of data is manipulated in the computer depends on its data type: When data is stored in
a variable it is usually converted to a binary number. The integer ‘2’ will be stored differently
compared to the string ‘2’. Their binary values will differ. With the integer, the computer would be
able to perform arithmetic on the binary value, however, with the string, it would not.
Computer Science UK Membership Site Licence: Do not share outside your centre. 2
A-Level Computer Science
There are a range of different data types which are described in the table opposite. You will need to know
these 5 data types, examples of the data that they hold and have a good idea as to their required size in
memory.
Real -12
Decimal or Whole 23,456
(float in Python) 4 or 8 bytes
number -0.34
1243.5434523
Collection of alpha-
“Adsh 889wd”
Usually 1 byte per numeric characters,
Strings character whitespace and
“sdsd34@@$”
“Pea Soup”
punctuation.
Questions
1. What is a variable? [2]
2. What is a constant? [2]
3. State the ‘data types’ required for the following variables:
a. student_name [1]
b. student_age [1]
c. student_sex [1]
d. student_height [1]
e. student_first_initial [1]
4. What is the difference between a variable and a constant and why might both be required in a
program? Give examples to support your answer. [4]
5. Why is important for a program to declare the data types of variables before the variables are used?
[4]
6. A program requires a variable to store an individual’s sex (m/f). Discuss whether ‘character’ or
‘Boolean’ would be the most suitable datatype in this situation. [4]
7. A programmer has been asked to write an accounting application to calculate how a business’
takings are taxed. Discuss the use of constants in a program such as this.[5]
Computer Science UK Membership Site Licence: Do not share outside your centre. 3
A-Level Computer Science
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
Computer Science UK Membership Site Licence: Do not share outside your centre.
4
A-Level Computer Science
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
Computer Science UK Membership Site Licence: Do not share outside your centre.
5
A-Level Computer Science
State/Identify/Give/Name: Simply label a diagram, fill out a table or write a few words
Describe: Describing is ‘saying what you see’ (E.G.: A computer will have a CPU, Primary and Secondary storage etc)
Explain: Explaining is ‘saying WHY/HOW something is like that’. (E.G.: A computer will have a CPU so that it can process all of the data the
computer needs to perform a range of tasks. Primary and Secondary storage is needed because…)
Discuss: Discussing is ‘looking at two sides of an issue, weighing up the two views and giving a conclusion’. Often these require a mini essay
answer. (E.G.: New technology could be seen as being bad for the environment because…, but on the other hand, new technology has led to…
In conclusion I believe that…)
Describe/Explain/Discuss using examples: Finally, if you are asked to give examples in any of these types of questions – YOU MUST GIVE
EXAMPLES!
Computer Science UK Membership Site Licence: Do not share outside your centre. 6
A-Level Computer Science
Further thoughts:
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
Computer Science UK Membership Site Licence: Do not share outside your centre. 7