Data Types (OCR GCSE Computer Science)

Revision Note

Robert Hampton

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

int_value = 5

real_value = float(int_value)

5.0

From Real to Integer

real_value = 5.7

int_value = int(real_value)

5

From String to Integer

str_value = "10"

int_value = int(str_value)

10

From Integer to String

int_value = 5

str_value = str(int_value)

"5"

From Boolean to String

bool_val = True

str_val = str(bool_val)

"True"

From String to Boolean

str_value = "True"

bool_val = bool(str_value)

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!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.