3.2.1 Data Types
Table of Contents
1 What are Data Types?
Learn It
Data can consist of numbers, characters, symbols, logical data or images. Data can be stored in many different forms called 'Data Types'. These forms determine what actions can be performed on the data when it is assigned to a variable in programming, or a field in a Database or Spreadsheet.
- All data, whether they be numbers, text, symbols, logical, images or video are stored as a series of binary digits,
1s
and0s
which the computer needs to know how tointerpret
. - In a computer program, a variable is used to hold data that can be used or manipulated in certain ways depending on the type of data it is assigned.
- Computers need to be told what type of data it is, so they know how it can be
manipulated
andrepresented
. - For Example:
- A student creates a variable called
FirstName
to hold the letters of a person's first name and they declare this variable's data type asString
. The computer will interpret the 1's and 0's asletters
. Even if it is anumber
, it will not work in amathematical equation
because thedata type
isString
. - The
Integer
5 * 5 = 25 but theString
'5' * 5 = '55555' - Similarly, a variable called
length
is declared asInteger
data type, and can be multiplied by another variable likewidth
to give the area of a shape.
- A student creates a variable called
- In some programming languages, like
Python
, variable data typesdo not need declaring
at all. The program will assume thedata type
of a variable based on what isput in it
. - For example, if you write (a = 21) and (b = "George") - a will be stored as an
integer
(whole number) and b will be stored as astring
(text). - Although you do not have to declare
variable data types
, you still need to understand the different data types and how to work with them in the programs your write.
2 Data Types In Programming
Learn It
- The table below details some of the main data types you will use:
The five main data types you will need to understand and use are:
- Integer.
- Real (Float).
- Character.
- String.
- Boolean.
Integer - Is a whole number which can be either positive (0 or above) or negative (under 0). Lets look at how this can be used in pseudocode and Python.
Real - Is any number (positive or negative) including decimal places. In Python areal number is called a floating-point number or float for short.
Character - Is a single symbol such as a letter. Take the word “hello” this could be defined as 5 single characters “h”, ”e”, “l”, “l”, and “o”.
String - Is a series of characters that are combined to make a single piece of data. They are defined by adding speech marks around the value. However, in Python anything that the user inputs is treated as a string text unless otherwise specified.
- Q1: In the example code above why is the data type not defined? Will this code work and why?
Boolean - Boolean values can have one of two values, usually True or False and cannot be left empty.
Casting - Casting is when you convert a variable value from one type to another. This is, in Python, done with functions such as int() or float() or str().
Badge It: Exam Questions
Silver - Answer the following questions:
- What is the most appropriate data type for each of these items? (4 Marks)
- a) The nickname of your best friend.
- b) The number on a rolled dice.
- c) The exact length of a car in metres.
- d) The answer to a yes/no question.
Upload to Fundamentals of 3.2 Programming - 3.2.1 Data Types: Silver on BourneToLearn
Badge It: Exam Questions
Gold - Answer the following questions:
- A pedestrian crossing uses a button to request the traffic to stop. State the data type that you would use to record each of these
variables and give reasons for your answer. (4 Marks)
- a) A variable to record whether the button has been pressed or not?
- Data Type:
- Reason:
- b) A variable to record how many whole seconds it's been since the button was pressed?
- Data Type:
- Reason:
- a) A variable to record whether the button has been pressed or not?
Upload to Fundamentals of 3.2 Programming - 3.2.1 Data Types: Gold on BourneToLearn
Badge It: Coding Challenge
Platinum - Using the pseudocode below, choose suitable variables and data types to create a working program to enrol new members to the club:
Upload to Fundamentals of 3.2 Programming - 3.2.1 Data Types: Platinum on BourneToLearn