75 exam-style questions on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.
Or take a quick quiz: 10 questions on this subtopic in GCSE Computer Science (Edexcel), marked as you tap.
Operators do the calculations and comparisons in every algorithm: arithmetic operators, relational (comparison) operators and the logical operators AND, OR and NOT. Questions ask you to evaluate expressions, especially ones with integer division and modulus, to decide whether conditions are True or False, and to write conditions of your own.
== equal to, != not equal to, < less than, > greater than, <= less than or equal to, >= greater than or equal to
and: True only if both are True. or: True if at least one is True. not: reverses
Precedence: brackets, then **, then * / // %, then + −
n % 2 == 0 means n is even
Notes
Arithmetic operators
diagram
+ add, - subtract, * multiply, / divide. In Python, / always gives a real (float) result: 7 / 2 is 3.5 and 8 / 2 is 4.0.
Integer division// gives the whole number of times one number goes into another: 17 // 5 is 3.
Modulus% gives the remainder: 17 % 5 is 2.
// counts the whole groups; % gives what is left over
Exponentiation** raises to a power: 2 ** 4 is 16.
Uses: n % 2 == 0 is True when n is even; n % 10 is the last digit of n; 135 minutes is 135 // 60 = 2 hours and 135 % 60 = 15 minutes.
Order: brackets, then **, then *///% (left to right), then + and - (left to right). So 3 + 2 ** 3 is 11.
Relational operators
They compare two values and give a Boolean result: True or False.
== equal to, != not equal to, < less than, > greater than, <= less than or equal to, >= greater than or equal to.
= assigns a value; == compares two values.
Translate words carefully: 'at least 18' is >= 18; 'over 18' is > 18; 'no more than 50' is <= 50.
Logical operators
diagram
AND(and): True only when both conditions are True.
OR(or): True when at least one condition is True.
NOT(not): reverses a condition, so True becomes False and False becomes True.
In range: age >= 13 and age <= 19. Out of range: mark < 0 or mark > 100.
and joins two tests to make a range between; or catches values outside at either end. A filled circle includes the value, an open circle leaves it out
Without brackets, not is done first, then and, then or. Use brackets to make the meaning clear, e.g. (day == "Sat" or day == "Sun") and not raining.Grade 7+ only
How to answer each type of question
Evaluate arithmetic expressions
1 mark eachGrade 5
Do brackets and ** first, then *,/,// and %, then + and -.
For // give only the whole-number part; for % give only the remainder.
Remember that / gives a real number in Python, e.g. 4.0 not 4.
Example. State the result of each expression. (a) 23 // 4 (b) 23 % 4 (c) 3 + 2 ** 3 (d) 10 / 4
Show the model answerHide the model answer
(a) 5
(1)
(b) 3
(1)
(c) 11 (1), because 2 ** 3 = 8 is worked out first
(d) 2.5
(1)
Decide whether a condition is True or False
1 mark eachGrade 6
Replace each variable with its value.
Work out each comparison to True or False.
Combine them: brackets first, then not, then and, then or.
Example.x = 8 and y = 3. State whether each condition is True or False. (a) x > 5 and y > 5 (b) not (x == y) (c) x % y == 2 or y > x
Show the model answerHide the model answer
(a) False (1), because y > 5 is False
(b) True (1), because x == y is False and not False is True
(c) True (1), because 8 % 3 is 2, so the first part is True
Write a condition
2 to 4 marksGrade 7
Turn each part of the rule into a comparison, choosing < or <= carefully at the boundaries.
Join the parts with and / or, and use brackets if you mix them.
Test it with a value just inside and a value just outside the range.
Example. A code is valid if it is between 1000 and 9999 inclusive and it is an even number. Write a Python if statement that outputs Valid when the integer variable code holds a valid code.
Show the model answerHide the model answer
if code >= 1000 and code <= 9999 and code % 2 == 0:
print("Valid")
Both boundary checks, >= 1000 and <= 9999 (1); tests for an even number with code % 2 == 0 (1); all three joined with and in an if statement that outputs Valid (1).
Use // and % to solve a problem
2 to 3 marksGrade 7
Use // for 'how many whole ...'.
Use % for 'how many are left over'.
Check your code with the example numbers in the question.
Example. A program inputs a whole number of minutes, e.g. 135. Write Python code that outputs the number of whole hours and the number of minutes left over, e.g. 2 hours 15 minutes.
Show the model answerHide the model answer
mins = int(input("Enter minutes: "))
hours = mins // 60
left = mins % 60
print(hours, "hours", left, "minutes")
Inputs the minutes as an integer (1); uses // 60 to find the whole hours (1); uses % 60 to find the minutes left over and outputs both (1).
Don’t lose marks
Using = instead of == in a condition.
Writing x > 5 and < 10. Each side of and needs a full comparison: x > 5 and x < 10.
Using and for an out-of-range check: mark < 0 and mark > 100 can never be True.
Getting boundaries wrong: 'at least 18' needs >=, not >.
Forgetting that ** is worked out before * and +, or that / gives a real number.
Mixing up // (whole number of times) and % (remainder).
More tips
Memory tricks
17 ÷ 5 = 3 remainder 2: integer division (//) gives the 3, modulus (%) gives the 2.
Even test: n % 2 == 0. Last digit: n % 10. Remove the last digit: n // 10.
One equals sign sets a value; two equals signs check it.
In range uses and. Out of range uses or, because a value cannot be too low and too high at once.
Exam technique
Underline the boundary words in the question (at least, more than, up to, inclusive) and choose the operator for each one.
Write down the value of each part of a condition, such as 8 % 3 = 2, so that you can check it.
Write logical operators the way the question does: AND, OR, NOT in a written answer, or and,or,not in Python.
What each grade needs
What you need to be able to do, from the first marks up to the top grade.
Grade 3
Use the four basic arithmetic operatorsAdd, subtract, multiply and divide, knowing that / gives a real number.
Grade 4
Use relational operators to compare valuesUse ==,!=,<,>,<= and >= to give True or False.
Grade 5
Calculate with modulus and integer division17 // 5 is 3 and 17 % 5 is 2.
Grade 5
Combine conditions with AND, OR and NOTJoin comparisons to test a range or several conditions at once.
Grade 6
Apply operator precedence to evaluate expressionsBrackets first, then exponentiation, then multiplication and division, then addition and subtraction.
Grade 7
Use modulus and integer division to solve problemsE.g. test whether a number is even, or convert minutes into hours and minutes.
Grade 8
Write complex conditions with brackets and NOTBuild conditions such as not (x == 0 or y == 0) that do exactly what is needed.
Quick recall
Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.
Write the Python operator that means less than or equal to.
<=
The variable door_open holds the value True. State the value of the expression not door_open.
False
The variables a and b each hold True or False. State when the expression a and b is True.
Only when both are True
Sample questions
Written for this site in the style of Edexcel exam questions. They are not taken from real past papers.
Question 1Easy5 marks
A teacher shares sweets equally between the children in a group. Any sweets left over are kept by the teacher.
01 sweets = 23
02 children = 4
03 each = sweets // children
04 left = sweets % children
05 print("Each child gets", each)
06 print(left, "sweets are left over")
(a) State the output of line 05.
[1]
(b) State the output of line 06.
[1]
(c) State the value of sweets / children.
[1]
(d) State the value of children ** 2.
[1]
(e) Give a reason why the programmer used // rather than / in line 03.
[1]
Show the answer and mark scheme
(a)Answer:Each child gets 5
Each child gets 5
(b)Answer:3 sweets are left over
3 sweets are left over
(c)Answer: 5.75
5.75
(d)Answer: 16
16
(e)Answer: // gives only the whole-number part of the division (5), because each child must get a whole number of sweets; / would give 5.75.
// gives only the whole-number part of the division, because the sweets cannot be split (/ would give 5.75)
Question 2Medium4 marks
This program converts a number of seconds into minutes and seconds.
01 total = int(input("Enter a number of seconds: "))
02 minutes = total // 60
03 seconds = total % 60
04 print(minutes, "minutes and", seconds, "seconds")
(a) State the values output for minutes and seconds when the user enters 200.
[2]
(b) Explain how the modulus operator can be used to check whether a whole number is even.
[2]
Show the answer and mark scheme
(a)Answer: 3 minutes and 20 seconds
minutes: 3
seconds: 20
(b)Answer: number % 2 gives the remainder after dividing by 2; a remainder of 0 means the number is even.
the number % 2 gives the remainder when the number is divided by 2
if the remainder is 0, the number is even (otherwise it is odd)
Question 3Hard5 marks
A three-digit whole number is stored in the variable number.
(a) Write an algorithm, in Python, that calculates and outputs the sum of the three digits of number. For example, if number is 472, the output is 13. You must use integer division and modulus.
[4]
(b) State the value of 472 // 10 % 10.
[1]
Show the answer and mark scheme
(a)Answer:
hundreds = number // 100
tens = (number // 10) % 10
units = number % 10
print(hundreds + tens + units)
hundreds digit found with number // 100
tens digit found with (number // 10) % 10 / (number % 100) // 10
units digit found with number % 10
adds the three digits and outputs the result
(b)Answer: 7
7
Worked solution: // and % are worked out from left to right: 472 // 10 = 47, then 47 % 10 = 7.