Program Structure, Data Types and Variables
Why Data Types Matter
Every value stored in a Python program has a data type. Choosing the right type is not optional — it determines what you can do with the data. You cannot multiply two strings together arithmetically. You cannot store the word “open” as a Boolean. Getting the type wrong causes errors that are often tricky to diagnose. Python works with four primitive data types you must know, plus structured types such as string, array and record.
The four primitive data types are: integer (whole number, e.g. 7 or -3), real (decimal number, e.g. 3.14 — Python calls this float), Boolean (True or False) and char (a single character, e.g. 'A'). A string (a sequence of characters, e.g. "hello") is a structured type, grouped with array and record. In your exam, use the term real rather than float in written answers.
The Primitive Data Types in Python
An integer stores a whole number. Examples include a player’s score, the number of students in a class, or a person’s age. In Python, you write: age = 16.
A real stores values with a fractional part. Examples include a temperature reading (36.7), a price (12.99), or a percentage (78.5). In Python: temperature = 36.7.
A Boolean holds only one of two values: True or False. Note the capital first letter — Python is case-sensitive. Examples: whether a user is logged in, whether a door sensor is triggered, whether a password matches.
A char is a single symbol such as 'A', '9' or '!'. Python has no separate character type: the PLS maps character to str, so a single character is simply a string of length 1, written in single or double quotes. A string is a sequence of characters — as long or short as needed, including an empty string "". In Python: name = "Priya".
When a user types a number using input(), Python receives it as a string by default. Trying to add two such strings gives concatenation, not arithmetic. For example: "5" + "3" gives "53", not 8. You must convert the input before doing any maths:
age = int(input("Enter your age: "))
year_of_birth = 2026 - age
print(year_of_birth)
Use int() for whole numbers and float() for decimals. This is one of the most common errors in exam answers — always check whether a variable storing user input has been converted to the right type.
Variables and Constants
A variable is a named location in memory whose value can change as the program executes. In Python, you create a variable simply by assigning a value to it: score = 0. Every variable has a name (its identifier), a value, and an implied data type.
A constant is a value that should stay fixed throughout the program. Python does not enforce constants with a keyword, but the convention is to write them in uppercase: MAX_ATTEMPTS = 3. Using a named constant makes maintenance far easier — if you later need to change the maximum to 5, you change it in one place rather than searching through every line of code for the number 3.
Meaningful identifier names make code readable and maintainable. livesRemaining is immediately clear; x or temp is not. The specification lists meaningful identifiers as one of the techniques that make a program easy to read, so use them whenever you write code. Single-letter names, apart from a conventional loop counter such as i, make code harder to follow.
Structured Data Types
Beyond the four primitives, Python supports structured types that group multiple values together. An array (implemented in Python as a list) holds an ordered sequence of values. A record groups related fields of possibly different types. In the PLS a record is written as a list, for example student = ["Priya", 15, True], and a table of records is a list of lists where every inner list has the same number of fields. These are covered in detail in a later lesson.
Writing Readable Code
The specification requires you to write code that is easy for another programmer to read. Four techniques contribute to readability: meaningful identifier names (already covered), correct indentation, useful comments, and consistent use of white space.
In Python, indentation is not optional — it defines code blocks. All statements inside an if, for or function body must be indented by the same amount (usually four spaces). Inconsistent indentation causes an IndentationError.
Hard to read (poor names, no comments, no spacing):
x=int(input(""))
if x>=50:
print("P")
else:
print("F")
Easy to read (meaningful names, comment at block level, spacing):
# Check whether a student has passed their exam
score = int(input("Enter exam score: "))
if score >= 50:
print("Pass")
else:
print("Fail")
Notice: the comment explains the purpose of the block, not what each individual line does. Over-commenting — adding a comment to every single line — makes code harder to read, not easier. A common mistake in exam answers is writing more comment text than code.
Initialisation and Assignment
Before you can use a variable, you must give it an initial value. This is called initialisation. For example, a score counter should start at zero: score = 0. Running total variables, list index counters and flag variables all need initialisation before a loop begins. Failing to initialise a variable causes a NameError in Python — the program tries to use a name it has never seen before.
When you encounter new code, try to predict its output before you run it. Consider:
total = 0
for i in range(1, 5):
total = total + i
print(total)
Predict: range(1, 5) produces 1, 2, 3, 4. The loop adds each to total, giving 1, 3, 6, 10. The output is 10. Then run it to verify. This predict-then-run habit is especially useful for tackling “Complete a Trace Table” questions in your exam.