2.1 Data Types Recap
Before you can write a real Paper 4 program, you need the building blocks: storing information, receiving it from a user, and making decisions. Python follows the same logic as pseudocode — the only difference is exact syntax.
Python has five data types you need for this syllabus:
| Type | Keyword | Example | Use |
|---|---|---|---|
| Integer | int | age = 17 | Whole numbers |
| Real | float | price = 3.99 | Decimal numbers |
| String | str | name = "Alice" | Text |
| Boolean | bool | isLoggedIn = True | True / False |
| None | NoneType | result = None | Placeholder (no value yet) |
- Always choose the most specific type.
- Use
int(notfloat) for whole numbers, andbool(notint) for True/False. - Using the wrong type does not always crash, but it loses the mark for that variable.
# Type casting — converting between types
x = "42" # str — cannot do arithmetic
y = int(x) # now y is the integer 42
z = float(x) # z is 42.0
int("15") # -> 15
float("3.14") # -> 3.14
str(100) # -> "100"
bool(0) # -> False
bool(1) # -> True- Name is text → str
- Exam score is a whole number → int
- Passed/failed is yes/no → bool
- Price has a decimal point → float
- Quantity is a whole number → int
- On sale is yes/no → bool
Data Types & Variables
2.2 Input & Output
Almost every Paper 4 program starts by reading values from the user and ends by printing results. Forgetting to convert input() is the single most common mistake in Paper 4.
Output — print() and f-strings
# Basic print
print("Hello, World!")
print(42)
# f-string — variables go inside { }
name = "Omar"
age = 16
print(f"Name: {name}, Age: {age}")Input — input() always returns a string
# input() always returns str — convert immediately if you need arithmetic
name = input("Enter your name: ") # str, no conversion
age = int(input("Enter your age: ")) # convert to int
price = float(input("Enter price: ")) # convert to floatinput()returns a string even if the user types a number."5" + "3"is"53"(concatenation), not 8.- Always wrap with
int()orfloat()when you need arithmetic.
- Name is str (no conversion needed)
- Year of birth must be int (to subtract from 2025)
- price → float
- quantity → int
- The single most common Paper 4 mistake is forgetting to convert
input(). - If your program does arithmetic with user input and you have not used
int()orfloat(), the marker will not award method marks for the calculation — even if the logic is correct.
Lab Tasks — Practice I/O
input() and Type Conversion
print() and f-strings
2.3 The if Statement Family
Conditional statements are how a program makes decisions. In Paper 4 you will almost always need at least one if. Cambridge marks carefully: correct indentation and the right branch structure are both required.
if — one branch
# Pseudocode: IF age >= 18 THEN ... ENDIF
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")if-else — two branches
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not old enough to vote.")- The code block inside an
ifmust be indented by 4 spaces. - This is how Python knows what belongs inside the block — there is no
ENDIFkeyword. - Forget the indentation and Python raises an
IndentationError.
- Use len() to find the length of a string
- len("hello") returns 5
if-elif-else — more than two outcomes
Use elif when there are more than two possible outcomes. Python tests conditions top to bottom and runs the first branch that is True — then skips the rest.
marks = int(input("Enter marks: "))
if marks >= 90:
print("Grade: A*")
elif marks >= 80:
print("Grade: A")
elif marks >= 70:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
else:
print("Grade: F")- Python checks conditions from top to bottom and stops at the first
Trueone. - Always order from most restrictive to least restrictive (e.g. highest marks to lowest).
- Getting the order wrong produces correct-looking code that gives wrong output.
- When Cambridge asks you to "write a conditional statement", always include every branch — do not leave out the
elseunless the question specifically requires only anif. - Missing a branch loses a mark.
if / if-else / if-elif-else
2.4 Nested if & Logical Operators
A nested if is an if placed inside another if block. Use nesting when you need to check a second condition that only makes sense once the first is already True.
age = int(input("Enter age: "))
hasLicense = input("Have a license? (yes/no): ")
if age >= 17:
if hasLicense == "yes":
print("You can drive.")
else:
print("You need a license to drive.")
else:
print("You are not old enough to drive.")Logical operators — and / or / not
Combine multiple conditions using these three operators:
| Operator | Meaning | Example |
|---|---|---|
and | All conditions must be True | age >= 18 and isRegistered |
or | At least one condition must be True | hasTicket or knowsHost |
not | Negates the condition | not isRaining |
Nested if and Logical Operators
2.5 Operators: //, %, and the Rest
Cambridge Paper 4 specifically tests // and % because they map directly to DIV and MOD in pseudocode.
Arithmetic operators
| Operator | Name | Example | Result | Pseudocode |
|---|---|---|---|---|
+ | Addition | 7 + 3 | 10 | + |
- | Subtraction | 7 - 3 | 4 | - |
* | Multiplication | 7 * 3 | 21 | * |
/ | Division (float) | 7 / 2 | 3.5 | / |
// | Integer division | 7 // 2 | 3 | DIV |
% | Modulus (remainder) | 7 % 2 | 1 | MOD |
** | Exponentiation | 2 ** 8 | 256 | ^ |
Comparison & logical operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 7 > 3 | True |
< | Less than | 2 < 1 | False |
>= | Greater or equal | 5 >= 5 | True |
<= | Less or equal | 4 <= 3 | False |
and | Both True | 5>3 and 2<4 | True |
or | At least one True | 5>10 or 2<4 | True |
not | Negates | not (5>3) | False |
DIV or MOD, always translate those to // and % in Python — never use int(a/b) as a substitute.Operators & //, % (DIV, MOD)
2.6 Exam Focus & Lab Tasks
Common mistakes that lose marks
| Mistake | What happens | Fix |
|---|---|---|
| Forgetting int() / float() around input() | TypeError or wrong string arithmetic | Always convert numeric inputs immediately |
= instead of == in a condition | SyntaxError or assignment instead of comparison | if x == 5: not if x = 5: |
| Wrong indentation | IndentationError — program crashes | 4 spaces per level; be consistent |
/ when integer result is needed | Gets a float (3.0) instead of int (3) | // for integer division — maps to DIV |
| Wrong order of elif branches | First True branch runs, wrong output | Order conditions carefully; test boundary values |
| Missing the else branch | Some inputs produce no output | Always include else unless question says otherwise |
Exam-style Lab Tasks
✓ Key Points Summary
2.7 Practice Tasks
Fifteen exam-style tasks. Click Hint for bullet-point guidance, then Help to reveal a worked Python solution.
Question Bank
Answer all questions, then press Submit Quiz to see your score.
Question 1Multiple Choice
Which data type is most appropriate for storing a temperature reading of 36.7?
Question 2True / False
input() returns a string even when the user types a number.
Question 3Multiple Choice
What is the output of: print(f"Total: {5 + 3}")
Question 4Multiple Choice
Which line correctly reads a decimal price from the user for arithmetic?
Question 5True / False
In an if-elif-else chain, Python runs the first branch whose condition is True and then skips the rest.
Question 6Multiple Choice
Which Python operator gives the remainder of 17 divided by 5?
Question 7Multiple Choice
Which is the correct way to check if two values are equal in an if condition?
Question 8True / False
The condition `age >= 18 and isRegistered` is True only if both parts are True.
Question 9Multiple Choice
What is 7 // 2 in Python?
Question 10Multiple Choice
A nested if is best used when:
Question 11True / False
Python uses indentation (4 spaces) instead of keywords like ENDIF to mark the end of an if block.
Question 12Multiple Choice
Which common Paper 4 mistake is caused by using / when an integer result is needed?
Answer all 12 questions to enable submission.