0% found this document useful (0 votes)
50 views7 pages

1.2.8.0 Variables, Constants and Data Types

The document introduces variables, constants, data types, inputs, and outputs in programming. It defines variables as memory locations that store data, and constants as variables that cannot change. Common data types include integers, floats, strings, Booleans, and characters. The document provides examples of each in Python.

Uploaded by

Zuhdija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

1.2.8.0 Variables, Constants and Data Types

The document introduces variables, constants, data types, inputs, and outputs in programming. It defines variables as memory locations that store data, and constants as variables that cannot change. Common data types include integers, floats, strings, Booleans, and characters. The document provides examples of each in Python.

Uploaded by

Zuhdija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

A-Level Computer Science

Variables, Constants and Data Types


Introduction
Over the next few slides, you will be introduced to the basics of programming. These basic aspects of
programming will be covered from theoretical point of view but there may be some practical examples
written in either pseudocode or the Python programming language

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”)

In this example, the input()


statement stops the window
closing too soon after the print
statement is executed

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.

Data Type Typical Size Explanation Example


104
Integers 2 or 4 bytes Whole Numbers 21
23,456

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.

Boolean TRUE / FALSE


Either TRUE or
1 bit ON / OFF
FALSE
1/0

Characters Single Character (any


‘1’
alphanumeric
‘D’
1 byte character or
‘%’
punctuation, but only
‘s’
one character)

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

Checklist: Keywords / Key Terms:


 Date and title, clearly presented Variable: A memory location containing a single piece of data that can change
 Spelling & grammar checked whilst the program is running.
Constant: A memory location containing a single piece of data that cannot
 Question numbers in the margin change whilst the program is running.
 Handwriting neat & legible Data Type: A formal description of the kind of data used in a computer program.
 Punctuation / Capital letters

_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
_____________________________________________________
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!

Stick answer sheet here

Computer Science UK Membership Site Licence: Do not share outside your centre. 6
A-Level Computer Science

Reflections: Score: / Percentage: % Grade: Progress: On / Above / Below

What Went Well?:  My answers effectively incorporated technical terminology.


 I demonstrated a good level of understanding.  My responses were well structured / organised.
 I responded to the command words effectively.  My revision strategy was effective as I showed depth of understanding in my
answers.
 My answers were detailed / were written in depth.
 My answers contained enough points / examples / explanations to achieve the
 My work was well presented / legible.
marks available.

Even Better If…:  I must incorporate key terminology into my answers.


 I must better organise my answers to improve its clarity.
 My answers need to be more accurate.
 I need to improve my revision strategy, as I did not demonstrate a depth of
 I must respond correctly to the command words. understanding in my answers.
 My answers need more detail / greater depth.  My answers didn’t contain enough points / examples / explanations to
 I must take greater care over my work / write neatly. achieve the marks available.

Further thoughts:
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________

Computer Science UK Membership Site Licence: Do not share outside your centre. 7

You might also like