Programming fundamentals
Variables, operators, input and output plus sequence, selection and iteration.
Learning Objectives
- 1Use variables, constants, operators, inputs, outputs and assignments
- 2Understand the three basic programming constructs: sequence, selection and iteration
- 3Recognise and use the required comparison, arithmetic and Boolean operators
Variables, constants and assignment
=) is how you store or update the value held in a variable.name = "Aleks". A constant stores a value that stays fixed throughout the program, e.g. const vat = 0.2. Assignment uses = to set or update a variable's value, e.g. score = score + 10 adds 10 to the current value of score. Input receives data from the user, and output displays data to the user.vat = 0.2
name = input("Enter your name: ")
score = 0
score = score + 10
print("Hello " + name)const vat = 0.2
name = input("Enter your name: ")
score = 0
score = score + 10
print("Hello " + name)Sequence, selection and iteration
total = 0
for i in range(1, 6): # count-controlled iteration
if i % 2 == 0: # selection
total = total + i
print(total) # sequence: runs after the loop finishestotal = 0
for i = 1 to 5 // count-controlled iteration
if i MOD 2 == 0 then // selection
total = total + i
endif
next i
print(total) // sequence: runs after the loop finishesArithmetic operators
+ add, - subtract, * multiply, / divide. Two less familiar ones are MOD, which gives the remainder of a division, and DIV, which gives the whole-number result of a division.+ (addition), - (subtraction), * (multiplication), / (division), MOD (modulo, the remainder after division), DIV (quotient, the whole number result of division), and ^ (exponentiation, to the power of). For example, 7 MOD 2 is 1 (the remainder), and 7 DIV 2 is 3 (the whole number of times 2 goes into 7).number MOD 2 == 0 is true for even numbers, since an even number always divides by 2 with no remainder. DIV and MOD together can decompose a division into its whole and remainder parts, useful for tasks like converting a total number of seconds into minutes and seconds.number = 13
if number % 2 == 0:
print("Even")
else:
print("Odd")
total_seconds = 125
minutes = total_seconds // 60
seconds = total_seconds % 60
# Python uses ** for exponentiation, not ^
squared = 4 ** 2 # 16number = 13
if number MOD 2 == 0 then
print("Even")
else
print("Odd")
endif
totalSeconds = 125
minutes = totalSeconds DIV 60
seconds = totalSeconds MOD 60
squared = 4 ^ 2 // 16Comparison operators
==), not equal to (!=), less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=).== equal to, != not equal to, < less than, <= less than or equal to, > greater than, >= greater than or equal to.=) means assignment; a double equals sign (==) means comparison, confusing these is a common syntax or logic error. When checking a value falls within an inclusive range (e.g. 1 to 100 inclusive), use >= and <=, not > and <, which would incorrectly exclude the boundary values themselves.Boolean operators and combining conditions
if age >= 18 AND height >= 155 then only lets someone through if both conditions hold.if PIN == "1234" OR "4321" then is a syntax error, since "4321" alone is not a valid comparison. The correct form repeats the variable: if PIN == "1234" OR PIN == "4321" then. This exact mistake, with AND or with OR, has been repeatedly identified by examiners across multiple series as a common error.age = int(input("Enter age: "))
height = int(input("Enter height (cm): "))
if age >= 18 and height >= 155:
print("Entry allowed")
pin = input("Enter PIN: ")
if pin == "1234" or pin == "4321":
print("Correct PIN")age = int(input("Enter age: "))
height = int(input("Enter height (cm): "))
if age >= 18 AND height >= 155 then
print("Entry allowed")
endif
pin = input("Enter PIN: ")
if pin == "1234" OR pin == "4321" then
print("Correct PIN")
endifExam Phrasing: Use These Exact Phrases
- ✓A variable stores data that can change while the program runs; a constant stores a fixed value
- ✓Sequence runs instructions in order, selection branches based on a condition, iteration repeats a block of code
- ✓MOD returns the remainder of a division; DIV returns the whole number result
- ✓Each condition joined with AND or OR must independently reference the variable being compared
- 💡Remember MOD (remainder) and DIV (whole number quotient) are separate operators, and MOD is a fast way to test odd/even.
- 💡When joining two conditions with AND or OR, always repeat the variable in full in both parts, e.g. pin == "1234" OR pin == "4321", not pin == "1234" OR "4321".
- 💡Do not confuse = (assignment) with == (comparison), they do very different things.
- 💡For an inclusive range (e.g. between 1 and 100 inclusive), use >= and <=, not > and <, which would wrongly exclude the boundary values.
Wrong:
if PIN == "1234" OR "4321" then
Right:
if PIN == "1234" OR PIN == "4321" then
Why it matters: The second part of the condition must also reference the variable, "4321" alone is not a valid, evaluable comparison.
Wrong:
x = 5 used to test if x equals 5
Right:
x == 5 used to test if x equals 5
Why it matters: A single = is assignment (sets the value); a double == is comparison (tests if two values are equal).
Wrong:
Renaming the variables a question gives you, e.g. writing word 1, wordone or world1 for word1
Right:
Use the exact variable names given in the question, spelled and spaced identically
Why it matters: OCR examiners were instructed not to allow renamed variables, even obvious typos. Free marks are lost to a stray space or a misspelling.
Wrong:
MOD and DIV do the same thing
Right:
MOD gives the remainder of a division; DIV gives the whole number result
Why it matters: 7 MOD 2 is 1 (the remainder); 7 DIV 2 is 3 (how many times 2 goes into 7).
Question:
Write an algorithm that asks the user to enter a number, then outputs whether the number is odd or even. [3]
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even")
else:
print("Odd")number = int(input("Enter a number: "))
if number MOD 2 == 0 then
print("Even")
else
print("Odd")
endif💡 Attempt this before revealing the answer, practice makes perfect!
Model Answer:
The algorithm takes a number as input, casts it to an integer, then uses MOD to check if dividing by 2 leaves a remainder. If number MOD 2 equals 0, the number is even; otherwise, it is odd.
Mark Scheme:
- 1Correctly takes input from the user and stores it in a variable
- 2Correctly uses the MOD operator to test divisibility by 2
- 3Correctly outputs "Even" or "Odd" depending on the result, using an IF/ELSE structure
Tap a card to flip it. Use the arrow keys to move and space to flip.
Exam-style questions. Have a go, then reveal the mark scheme to check your answer.
1.Explain the difference between a variable and a constant.
[2]2.State the value of 23 MOD 5 and the value of 23 DIV 5.
[2]3.A student writes the condition "if colour == 'red' OR 'blue' then" to check if colour is red or blue. Identify the error and rewrite the condition correctly.
[2]4.Name the three basic programming constructs, and give a one-sentence description of each.
[3]5.Write an algorithm that asks the user for a number of total seconds, then outputs the equivalent number of whole minutes and remaining seconds.
[3]total_seconds = int(input("Enter total seconds: "))
minutes = total_seconds // 60
seconds = total_seconds % 60
print(minutes, "minutes and", seconds, "seconds")totalSeconds = int(input("Enter total seconds: "))
minutes = totalSeconds DIV 60
seconds = totalSeconds MOD 60
print(str(minutes) + " minutes and " + str(seconds) + " seconds")6.Describe the three basic programming constructs, and explain why a program that validates a user's login needs all three.
[6]The examiners' reports are the most useful and least read of the three: they say what candidates actually got wrong each year. All files are hosted by OCR.
Past-paper questions grouped by topic
The same OCR questions, sorted by topic rather than by year (via Physics & Maths Tutor).
A common misconception affecting selection statements was joining two conditions with OR (or AND) without repeating the variable in the second condition, e.g. writing if PIN == "1234" OR "4321" then, which is not a valid comparison. The correct form is if PIN == "1234" OR PIN == "4321" then. This same error pattern was seen with AND earlier in the same paper, confirming it as a widespread, repeated misconception rather than a one-off mistake.
The use of += as an alternative to writing out position = position + 5 in full was confirmed by examiners as an entirely acceptable, equivalent way of expressing assignment with arithmetic, credited the same as the longer form.