Operators

GCSE Computer Science (Edexcel) · Edexcel 1CP2 · 6.5 · Problem solving with programming

48 exam-style questions plus unlimited generated ones 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: 11 questions on this subtopic in GCSE Computer Science (Edexcel), marked as you tap.

Revision notes

The operators Python uses to calculate, compare and combine conditions: arithmetic operators (including integer division, modulus and exponentiation), relational operators (such as == and !=) and logical operators (and, or, not). You need to work out the value of expressions and choose the right operator in your own code.

Key facts

  • + addition, - subtraction, * multiplication, / division (always gives a real)
  • // integer division: 17 // 5 = 3
  • % modulus (remainder): 17 % 5 = 2
  • ** exponentiation: 2 ** 3 = 8
  • == equal to, != not equal to, < less than, > greater than, <= less than or equal to, >= greater than or equal to
  • and: both True. or: at least one True. not: reverses True and False
  • Order: brackets, **, then * / // %, then + -
  • Even test: n % 2 == 0
  • = assigns a value; == compares values

Notes

Arithmetic operators

diagram
  • + addition, - subtraction, * multiplication, / division.
  • / always gives a real (float): 7 / 2 is 3.5 and 8 / 2 is 4.0.
  • // integer division: how many whole times one number goes into another. 17 // 5 is 3.
  • % modulus: the remainder after integer division. 17 % 5 is 2.
  • 17 // 5 = 33 whole groups of 517 % 5 = 22 left over
    17 shared into groups of 5: // counts the whole groups, % gives what is left over.
  • ** exponentiation (to the power of): 2 ** 5 is 32.
  • Order: brackets, then **, then *, /, // and % (left to right), then + and -. So 3 + 4 * 2 is 11 but (3 + 4) * 2 is 14.

Using // and %

  • Even or odd: n % 2 == 0 is True when n is even.
  • Divisible: n % 7 == 0 is True when n divides exactly by 7.
  • Units: 200 minutes is 200 // 60 = 3 hours and 200 % 60 = 20 minutes.
  • Digits: n % 10 is the last digit of a whole number and n // 10 removes it. For 472 these give 2 and 47. Grade 7+ only

Relational operators

  • == equal to, != not equal to, < less than, > greater than, <= less than or equal to, >= greater than or equal to.
  • Every comparison gives a Boolean result: 5 >= 5 is True and 5 > 5 is False.
  • = assigns a value; == compares two values. if score = 10: is a syntax error.
  • Strings can be compared too, but it is case-sensitive: "Yes" == "yes" is False.

Logical operators

  • and: True only if both conditions are True. or: True if at least one condition is True. not: reverses a Boolean, so not True is False.
  • Inside a range: mark >= 0 and mark <= 100. Outside a range: mark < 0 or mark > 100.
  • Write every comparison in full: choice == "A" or choice == "B", not choice == "A" or "B". Grade 7+ only
  • Python applies not first, then and, then or. Use brackets to make the order you want clear, e.g. (day == 6 or day == 7) and not raining. Grade 8+ only

How to answer each type of question

State the value of arithmetic expressions

1 mark eachGrade 5
  1. Put the values of the variables into the expression.
  2. Work out brackets first, then **, then *, /, // and %, then + and −.
  3. Remember that / always gives a real, so write 4.0, not 4.

Example.

a = 23
b = 4
State the value of each expression.
(a) a // b (1)
(b) a % b (1)
(c) a / 2 (1)
(d) b ** 2 + 1 (1)
(e) (a - 3) // b (1)

Show the model answerHide the model answer
(a) 5
(1)
(b) 3
(1)
(c) 11.5
(1)
(d) 17
(1)
(e) 5, because 20 // 4 = 5
(1)

State whether conditions are True or False

1 mark eachGrade 6
  1. Work out each comparison on its own and write T or F above it.
  2. Then apply not, then and, then or (brackets first).
  3. Remember that comparing strings is case-sensitive.

Example.

x = 8
y = 3
name = "Ali"
State whether each condition is True or False.
(a) x != y (1)
(b) x > 5 and y > 5 (1)
(c) x > 5 or y > 5 (1)
(d) not (x % y == 2) (1)
(e) name == "ali" (1)

Show the model answerHide the model answer
(a) True
(1)
(b) False
(1)
(c) True
(1)
(d) False, because 8 % 3 is 2, so the bracket is True
(1)
(e) False, because the comparison is case-sensitive
(1)

Write code using // and %

3 marksGrade 6
  1. Use // to find how many whole units fit.
  2. Use % to find what is left over.
  3. Output both parts in the form the question asks for.

Example. The integer variable seconds holds a length of time, e.g. 500.
Write code that outputs the time in minutes and seconds, e.g. 8 minutes 20 seconds. (3)

Show the model answerHide the model answer
minutes = seconds // 60
left = seconds % 60
print(minutes, "minutes", left, "seconds")
Uses // 60 to find the whole minutes (1); uses % 60 to find the seconds left over (1); outputs both values with the words minutes and seconds (1).

Write a condition using logical operators

3 to 4 marksGrade 7
  1. Write each part of the rule as a full comparison.
  2. Join the parts with and (all must be true) or or (any can be true).
  3. Add brackets so that or is worked out before and when the rule needs it.

Example. A student may enter a competition if they are in year 10 or year 11 and they have not entered before. The year is stored in the integer year, and entered is True if the student has entered before.
Write an if statement that outputs You may enter or You cannot enter. (4)

Show the model answerHide the model answer
if (year == 10 or year == 11) and not entered:
    print("You may enter")
else:
    print("You cannot enter")
Checks the year is 10 or 11, with both comparisons written in full (1); checks the student has not entered, e.g. not entered or entered == False (1); brackets and and used so the year test is worked out first (1); correct message in each case using else
(1).
Without the brackets, a year 10 student who has entered before would be allowed in.

Don’t lose marks

  • Writing that 17 / 5 is 3. It is 3.4; use // for whole-number division.
  • Using = instead of == in a condition.
  • Writing choice == "A" or "B": this is always True, because a non-empty string on its own counts as True.
  • Using and where you need or: mark < 0 and mark > 100 can never be True.
  • Forgetting that comparing strings is case-sensitive.
  • Ignoring the order of operations: 2 + 3 ** 2 is 11, not 25.

More tips

Memory tricks

  • // asks 'how many whole times?'; % asks 'how much is left over?'
  • Check: for positive whole numbers, (a // b) * b + a % b always equals a. For 17 and 5: 3 × 5 + 2 = 17.
  • and is fussy (needs both); or is easy-going (needs just one).
  • Inside a range, join with and; outside a range, join with or.
  • One equals sign sets a value; two equals signs ask a question.

Exam technique

  • When stating output, remember that / always gives a real: write 4.0, not 4.
  • For True or False questions, work out each comparison separately before applying not, and, or.
  • Name operators with the specification's terms: modulus, integer division, exponentiation.
  • Test every condition you write with a value on each side of the boundary.

What each grade needs

What you need to be able to do, from the first marks up to the top grade.

  1. Grade 3
    Use +, −, * and / correctlyKnow that / always gives a real result, e.g. 8 / 2 gives 4.0.
  2. Grade 4
    Use the six relational operatorsCompare values with ==, !=, <, >, <= and >= to get True or False.
  3. Grade 5
    Use integer division and modulus17 // 5 gives 3 (the whole number part) and 17 % 5 gives 2 (the remainder).
  4. Grade 5
    Combine conditions with and, or, notE.g. age >= 13 and age <= 19 is True only for ages 13 to 19.
  5. Grade 6
    Apply the order of operationsBrackets first, then exponentiation, then *, /, // and %, then + and −.
  6. Grade 7
    Use modulus to test divisibilityn % 2 == 0 tests for an even number; n % 10 gives the last digit.
  7. Grade 8
    Evaluate complex Boolean expressionsKnow that Python applies not first, then and, then or, and use brackets to control the order.

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

Give the logical operator that joins two conditions so that the result is True only when both conditions are True.
and
State the data type of the result of a comparison such as age >= 16.
Boolean
All three errors in this program are the same type of error. State the type of error.
Logic error.
The operators ==, !=, < and >= are all the same type of operator. State the name of this type of operator.
Relational (comparison) operators.
The ………… operator gives True only when both conditions are True.
and

Sample questions

Written for this site in the style of Edexcel exam questions. They are not taken from real past papers.

Question 1Easy4 marks
A program should compare two numbers and state their average and whether they are the same. The program below does this, but it contains errors.
01 # Average of two numbers
02 # Works out the average of two numbers and compares them
03 print("Number comparison tool")
04 print("------------------------")
05 num1 = int(input("Enter first number: "))
06 num2 = int(input("Enter second number: "))
07 average = num1 + num2 / 2
08 print("Number 1:", num1)
09 print("Number 2:", num2)
10 print("The average is", average)
11 if num1 = num2:
12     print("The numbers are the same")
13 else:
14     print("The numbers are different")
15 print("Thank you for using the tool")
(a) Amend the program to:
  1. fix the syntax error on line 11
  2. fix line 07 so that the average is correctly calculated as (num1 + num2) ÷ 2.
Test your amended program with the numbers 4 and 6.
[2]
(b) Once the syntax error is fixed but before line 07 is corrected, the program outputs The average is 7.0 for the numbers 4 and 6. Explain why.
[2]
Show the answer and mark scheme
(a) Answer:
# Average of two numbers
# Works out the average of two numbers and compares them
print("Number comparison tool")
print("------------------------")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
average = (num1 + num2) / 2
print("Number 1:", num1)
print("Number 2:", num2)
print("The average is", average)
if num1 == num2:
    print("The numbers are the same")
else:
    print("The numbers are different")
print("Thank you for using the tool")
  • line 11: changes = to ==
  • line 07: adds brackets, (num1 + num2) / 2
(b) Answer: Division has a higher precedence than addition, so num2 / 2 is worked out first: 4 + 6 ÷ 2 = 4 + 3 = 7.0.
  • division (/) is carried out before addition (+), because it has a higher precedence
  • so the program works out 4 + (6 ÷ 2) = 4 + 3 = 7.0 instead of (4 + 6) ÷ 2 = 5.0
Question 2Medium7 marks
Look at this program.
01 number = int(input("Enter a whole number: "))
02 if number % 2 == 0:
03     print("Even")
04 else:
05     print("Odd")
06 print("Last digit:", number % 10)
07 print("Tens digit:", number // 10 % 10)
(a) The user enters 347. State the output of the program.
[3]
(b) Explain how the expression number // 10 % 10 finds the tens digit of the number.
[2]
(c) Write an if statement that outputs Fizz when number is a multiple of 3.
[2]
Show the answer and mark scheme
(a) Answer:
Odd
Last digit: 7
Tens digit: 4
  • Odd
  • Last digit: 7
  • Tens digit: 4
(b) Answer: // 10 removes the last digit (347 becomes 34). % 10 then gives the last digit of what is left (4), which was the tens digit. // and % have the same precedence, so they are carried out from left to right.
  • // 10 removes the last digit / divides by 10 ignoring the remainder (347 → 34)
  • % 10 then gives the last digit of the result (34 → 4), which is the tens digit
(c) Answer:
if number % 3 == 0:
    print("Fizz")
  • uses number % 3 == 0 as the condition
  • outputs Fizz inside the if statement
Question 3Hard6 marks
01 n = int(input("Number: "))
02 bits = ""
03 while n > 0:
04     bits = str(n % 2) + bits
05     n = n // 2
06 print(bits)
Explain how this program works. In your answer, refer to the operators used on lines 03, 04 and 05 and state the output when the input is 13.
[6]
Show the answer and mark scheme
Answer: It converts a denary number to binary by repeatedly taking the remainder after dividing by 2 and halving the number. For 13 the output is 1101.
  • the program converts a (positive) whole number from denary into binary
  • n % 2 gives the remainder when n is divided by 2, which is the right-most (lowest) binary digit
  • str() turns the remainder into text so that it can be joined to the string bits
  • the new digit is added in front of the digits already found, so the first digit found ends up on the right
  • n // 2 divides by 2 and discards the remainder, which removes the digit just found
  • n > 0 keeps the loop repeating until all the digits have been found (n has reached 0)
  • for an input of 13 the output is 1101

Marked with levels of response: the full level descriptors are in the app.

More in Problem solving with programming

Stuck? Get 1-to-1 help.

Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.