Skip to content

You are viewing a free preview of this lesson.

Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.

Variables, Constants, and Data Types

Variables, Constants, and Data Types

This lesson covers the fundamental building blocks of every program — variables, constants, and data types. Understanding these concepts is essential for GCSE Computer Science (AQA 3.2 / OCR J277 2.2) and forms the foundation of all programming topics.


What Is a Variable?

A variable is a named location in memory that stores a value which can change during the execution of a program. Think of it as a labelled box — the label is the variable name, and the contents of the box is the value.

Declaring and Assigning Variables

In pseudocode (AQA style):

name ← "Alice"
age ← 16
score ← 0

In Python:

name = "Alice"
age = 16
score = 0

The assignment operator (← in pseudocode, = in Python) stores the value on the right into the variable on the left. Variables can be reassigned at any time:

score = 10
score = score + 5  # score is now 15

Naming Rules

  • Variable names should be meaningful (e.g. totalScore not x).
  • They must start with a letter (not a number).
  • They cannot contain spaces — use camelCase (totalScore) or snake_case (total_score).
  • They cannot be reserved words such as if, while, print.

Exam Tip: The examiner wants you to use meaningful identifiers. A variable called t will often lose you marks compared with temperature.


What Is a Constant?

A constant is a named value that is set once and cannot be changed during the execution of the program. Constants make code easier to read and maintain.

In pseudocode:

CONST VAT_RATE ← 0.20
CONST PI ← 3.14159

In Python (by convention — Python does not enforce constants):

VAT_RATE = 0.20
PI = 3.14159

Why Use Constants?

Benefit Explanation
Readability VAT_RATE is clearer than 0.20 scattered through code
Maintainability Change the value in one place and it updates everywhere
Error prevention Prevents accidental modification of important values

Exam Tip: If a question asks the difference between a variable and a constant, state that a variable's value can change during execution whereas a constant's value is fixed once assigned.


Data Types

A data type defines the kind of value a variable can hold. The five data types you must know for GCSE are:

Data Type Description Example
Integer A whole number (positive or negative) 42, -7, 0
Real / Float A number with a decimal point 3.14, -0.5, 100.0
Boolean A value that is either True or False True, False
Character A single letter, digit, or symbol 'A', '7', '!'
String A sequence of characters "Hello", "GCSE2025"

Integer vs Real

An integer has no fractional part: 5, -12, 0. A real (also called a float) has a decimal point: 5.0, -12.75. This distinction matters because integers use less memory and arithmetic with them is faster.

Boolean

A Boolean can only hold one of two values: True or False. Booleans are central to selection and iteration because conditions evaluate to a Boolean result.

is_logged_in = True
has_passed = score >= 50  # evaluates to True or False

Character vs String

A character (char) is a single symbol, whereas a string is zero or more characters in sequence. In Python, there is no separate char type — a single character is simply a string of length 1.

letter = 'A'         # character (string of length 1 in Python)
greeting = "Hello"   # string of length 5

Casting (Type Conversion)

Sometimes you need to convert between data types. This is called casting.

age = int("16")        # string → integer
price = float("9.99")  # string → real
label = str(42)        # integer → string

In pseudocode:

age ← INT("16")
price ← REAL("9.99")
label ← STRING(42)

Casting is essential when reading user input, because input() in Python always returns a string:

age = int(input("Enter your age: "))

Exam Tip: A very common error in exam pseudocode answers is forgetting to cast the input to an integer before comparing it with a number. Always show the cast explicitly.


Summary

  • A variable stores a value that can change; a constant stores a value that stays the same.
  • The five GCSE data types are integer, real/float, Boolean, character, and string.
  • Casting converts values between data types.
  • Use meaningful names for variables and constants.
  • Constants improve readability, maintainability, and error prevention.