GCSE Computer Science

Python variables and data types โ€” GCSE Computer Science

What is a variable?

A variable is a name that holds a value. When the value changes, the name stays the same. That's all a variable is โ€” a label attached to a piece of data so you can refer to it later.

The reason variables matter in GCSE Computer Science isn't philosophical. It's practical: without variables, a program can't remember anything. Every piece of data that a program stores, processes, or outputs lives in a variable at some point.

Data types

age = 16          # integer
name = "Alice"    # string
height = 1.65     # float
is_enrolled = True  # boolean

Python assigns a data type automatically based on the value. An integer is a whole number. A float is a decimal number. A string is text โ€” always wrapped in quotes. A boolean is either True or False. GCSE exams expect you to identify the correct data type for a given situation and explain why.

Type casting

age = input("Enter your age: ")  # age is a string
age = int(age)                   # now age is an integer
print(age + 1)

input() always returns a string โ€” even if the user types a number. To do arithmetic with it, you need to convert it. int() converts to an integer. float() converts to a decimal. str() converts back to a string. Getting this wrong is one of the most common causes of runtime errors in GCSE exam answers.

Common exam mistake

Using input() without int() or float() wrapping when a number is needed, then being surprised that "5" + 3 raises a TypeError. Type casting is tested on every board.

What examiners actually test

Variable questions appear in three forms: identifying the data type of a given value, explaining what a variable stores at a specific point in a program (often as part of a trace table), and correcting a type error in given code. The casting question โ€” where input() returns a string that needs converting before arithmetic โ€” appears so consistently it is worth memorising as a pattern.

Casting errors don't go away after one lesson โ€” they resurface in every piece of coursework until a student's had enough reps to make it automatic. That's what the 195 challenges across Python Coach's 27 lessons are for, with progress tracking so you can see who needs another pass. Free for 60 teaching days, no card needed.

Start your school's free trial →