Data Types (OCR GCSE Computer Science)
Revision Note
Written by: Robert Hampton
Reviewed by: James Woodhouse
Primitive Data Types
What is a data type?
A data type is a classification of data into groups according to the kind of data they represent
Computers use different data types to represent different types of data in a program
The basic data types include:
Data type | Used for | Example |
---|---|---|
Integer | Whole numbers | 10, -5, 0 |
Real | Numbers with a fractional part | 3.14, -2.5, 0.0 |
Character | Single character | 'a', 'B', '6', '£' |
String | Sequence of characters | "Hello world", "ABC", "@#!%" |
Boolean | True or false values | True, False |
It is important to choose the correct data type for a given situation to ensure accuracy and efficiency in the program
Data types can be changed within a program, this is called casting
What is casting?
Casting is when you convert one data type to another data type
Example
The following Python program is used to capture a users age to determine if they are old enough to vote
Line | Python code |
---|---|
01 | age = input("Enter age") |
02 | if age >= 18: |
03 | print("Old enough to vote") |
04 | else: |
05 | print("Too young to vote") |
In this example, on line 01, no specific data type is requested
By default the data type is stored as 'string'
On line 02, a run-time error would occur because age is stored as a string and is being compared to an integer value in the selection statement
Casting the age from a string to an integer would solve the error
Line | Python code |
---|---|
01 | age = input("Enter age") |
02 | if int(age) >= 18: |
03 | print("Old enough to vote") |
04 | else: |
05 | print("Too young to vote") |
In the corrected code, casting is highlighted in green
Casting between data types
Conversion | Example | Output |
From Integer to Real |
| 5.0 |
From Real to Integer |
| 5 |
From String to Integer |
| 10 |
From Integer to String |
| "5" |
From Boolean to String |
| "True" |
From String to Boolean |
| True |
Worked Example
Customers booking a holiday can choose between half board or all inclusive and a hotel star rating between 1 and 5
A typical booking record is shown in the table:
firstName | Jacob |
lastName | Franks |
boardType | All inclusive |
starRating | 5 |
bookingComplete | True |
State the most appropriate data type for the following fields [2]:
boardType |
|
starRating |
|
Give the name of one field that could be stored as a Boolean data type [1]
Answer
boardType | String |
starRating | Integer |
bookingComplete
Last updated:
You've read 0 of your 5 free revision notes this week
Sign up now. It’s free!
Did this page help you?