Data Types (OCR GCSE Computer Science)

Revision Note

Flashcards
Robert Hampton

Expertise

Computer Science Content Creator

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

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

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

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.