Data types
x

Revision notes for OCR GCSE Computer Science Data types. Open the guide for explanations and worked examples. Written against the OCR GCSE Computer Science (J277) specification, so the content matches what's examinable rather than general Computer Science background.

Data types

What you'll learn

  • What a data type is and why programs need data types.
  • How to use integer, real, Boolean, character and string data.
  • How to choose a suitable data type for a given scenario.
  • How casting can temporarily change a value’s data type.

Why data types matter

A program stores and processes values. A value is a single piece of data, such as 42, "Alex", True or 3.75.

A variable is a named place in memory that stores a value while a program runs. For example, score = 10 stores the value 10 in a variable called score.

Definition

Data type

A data type describes what kind of value is being stored and what operations can sensibly be performed on it.

For example, 10 + 5 should do number addition, but "10" + "5" may join text together to make "105" in many high-level languages.

A high-level language is a programming language designed to be easier for humans to read and write than machine code. Python is a common classroom example, but OCR may also accept clear pseudocode or other high-level languages.

Key Idea

Types control meaning

The same-looking data can behave differently depending on its type. 7 as an integer is a number; "7" as text is a character or string.

The main data types

Integer

An integer is a whole number with no decimal point. It can be positive, negative or zero.

Examples:

  • 0
  • 12
  • -5
  • 2048

In Python, you might write:

PurposeExample
Player scorescore = 0
Number of attemptsattempts = 3
Temperature rounded to whole degreestemperature = -2

Integers are suitable when the value is counted in whole units, such as lives in a game, number of students, stock quantity or goals scored.

Real

A real number can include a decimal part. In Python, this is usually stored using a type called float, but the GCSE term you should know is real.

Examples:

  • 3.5
  • 0.75
  • -2.4
  • 19.99

Use a real data type when fractional values are possible, such as distance, height, mass, time or a measurement from a sensor.

Common Mistake

Using integer for measurements

If a value might need a decimal part, do not choose integer. For example, a person’s height could be 1.72, so real is more suitable than integer.

Boolean

A Boolean value can only be one of two values: True or False.

Booleans are used for decisions. They often store whether something is currently true in a program.

Examples:

PurposeBoolean variable
User has logged inisLoggedIn = True
Game has endedgameOver = False
Password is correctpasswordCorrect = True

A condition is an expression that evaluates to a Boolean value. For example:

  • age >= 18
  • score == 0
  • username != ""

You can combine Boolean values using Boolean operators:

  • AND means both parts must be true.
  • OR means at least one part must be true.
  • NOT reverses the truth value.
Tip

Boolean variable names

Boolean variables are often clearer if they sound like yes/no questions, such as hasTicket, isMember or gameOver.

Example

Evaluating a Boolean condition

Suppose a cinema program uses this condition:

canEnter = age >= 13 OR hasAdult == True

For age = 11 and hasAdult = True:

  1. Evaluate the first comparison: age >= 13 becomes 11 >= 13, which is False.
  2. Evaluate the second comparison: hasAdult == True becomes True == True, which is True.
  3. Apply OR: False OR True gives True, so canEnter is True.

Character

A character is a single symbol, such as:

  • "A"
  • "7"
  • "?"
  • " " for a space character

Characters can be letters, digits, punctuation marks or other symbols.

In some languages, characters are written using single quotes, such as 'A'. In Python, there is no separate character type: a single character is stored as a string of length 1.

String

A string is a sequence of characters. It is used for text.

Examples:

  • "Hello"
  • "Alex"
  • "A123"
  • "07 1234 5678"
  • "" for an empty string

A string can contain letters, digits, spaces and punctuation. Digits inside a string are not treated as numbers for arithmetic.

A concatenation is when strings are joined together. For example, "Comp" + "uter" makes "Computer".

Common Mistake

Treating all digits as numbers

Phone numbers, postcodes and usernames are usually strings, not integers. You do not do arithmetic with them, and they may contain spaces, letters, symbols or leading zeroes.

Choosing suitable data types

In exam questions, you may be given a scenario and asked to choose data types for different pieces of data.

Use these questions:

QuestionLikely data type
Is it a whole number used for counting?Integer
Can it have a decimal part?Real
Is it only true or false?Boolean
Is it one symbol?Character
Is it text, an ID, a phone number or a code?String
Example

Choosing data types for a user profile

A program stores details about a user: age, height in metres, whether they accept emails, middle initial and mobile number.

  1. age is a count of completed years and should not have a decimal part, so integer is suitable.
  2. heightMetres might be 1.68 or 1.75, so real is suitable.
  3. acceptsEmails has only two possible states, True or False, so Boolean is suitable.
  4. middleInitial is one symbol, such as "K", so character is suitable.
  5. mobileNumber may contain a leading 0, spaces or +44, and it is not used for arithmetic, so string is suitable.

Practical use in a high-level language

Here are the GCSE data types shown using Python-style examples:

GCSE data typePython-style exampleNotes
Integerlives = 3Whole number
Realdistance = 4.5Number with a decimal part
BooleanisOpen = TrueEither True or False
Charactergrade = "A"Python stores this as a one-character string
Stringname = "Sam"Sequence of characters

Notice the quotation marks:

  • 25 is an integer.
  • "25" is a string.
  • True is a Boolean.
  • "True" is a string containing the characters T, r, u, e.

Casting

Sometimes a value is stored as one type, but you need to use it as another type. Casting means converting a value from one data type to another.

Definition

Casting

Casting is temporarily changing a value from one data type to another, usually so that a particular operation can be carried out.

Common examples in Python include:

CastMeaningExample
int(...)Convert to integerint("15") gives integer 15
float(...)Convert to realfloat("1.75") gives real 1.75
str(...)Convert to stringstr(42) gives string "42"

Casting is especially useful with user input. In Python, input() gives a string, even if the user types digits.

Example

Converting input before calculating

A program asks the user for two numbers. The user types 12 and 3.

  1. The values from input() are strings, so they are initially stored like "12" and "3".
  2. If the program uses "12" + "3", it may concatenate the strings to make "123" instead of adding the numbers.
  3. Cast each input before calculating: num1 = int(num1Text) and num2 = int(num2Text).
  4. Now num1 + num2 gives integer 15, so the program can calculate correctly.
  5. If the result must be displayed inside a message, cast it back to a string, such as "Total: " + str(total).
Common Mistake

Thinking casting always changes the original

A cast usually produces a converted value. The original variable is not changed unless you store the converted value back into a variable.

For example, int(ageText) creates an integer version of ageText, but ageText itself is still a string unless you assign the result, such as age = int(ageText).

Common Mistake

Casting must be possible

Not every cast makes sense. For example, trying to convert "twelve" into an integer would fail because it is not written as digits.

Quick comparison

DataBest typeReason
17 as someone’s ageIntegerWhole number count
2.5 hoursRealHas a decimal part
False for game overBooleanOnly true or false
"M" as a size codeCharacterOne symbol
"M12 4AB" as a postcodeStringText/code, not arithmetic
Exam technique

In the exam

  1. Ask what the data represents, not just what it looks like. A phone number may look numeric but should usually be a string.
  2. Use the OCR terms: integer, real, Boolean, character, string and casting.
  3. When explaining casting, say why it is needed, such as converting input text to a number before doing a calculation.
Self review

Check yourself

  • What data type would you choose for a product price, and why?
  • Why is "123" not the same as 123 in a program?
  • Give one situation where casting would be useful.

Recap questions

Test yourself with 5 quick questions on this guide. Answer them all correctly to complete it.

You've reached the end

Test yourself on this topic, or move on to the next guide.

Practice questionsTake a quick quiz on this topicFlashcardsSelf-test with active recall
Additional programming techniquesUp next

How was this guide?

Data types Revision Guide

  1. GCSE
  2. /Computer Science
  3. /Data types