x

Revision notes for Edexcel GCSE Computer Science Using relational operators. Open the guide for explanations and worked examples. Written against the Edexcel GCSE Computer Science (1CP2) specification, so the content matches what's examinable rather than general Computer Science background.

Using relational operators

What you'll learn

  • How relational operators compare values and produce True or False.
  • The Python 3 symbols for equal to, less than, greater than, and their variants.
  • How to use comparisons inside if, elif, else and while.
  • How to avoid common boundary and equality mistakes.

Start point: values, variables and Boolean results

A value is a piece of data, such as 42, "yes" or True.

A variable is a named storage location for a value, such as score or username.

A data type is the kind of data a value has. For example:

  • an integer is a whole number, such as 17
  • a string is text, usually written in quotation marks, such as "Sam"
  • a Boolean value can only be True or False

An expression is a piece of code that works out to a value. For example, score + 5 is an expression because Python can calculate its result.

What is a relational operator?

Definition

Relational operator

A relational operator compares two values and returns a Boolean result: either True or False.

A comparison such as score >= 50 is called a relational expression. It asks a yes/no question: “Is score greater than or equal to 50?”

Example

Evaluating simple comparisons

Suppose age = 14, attempts = 3 and username = "sam".

  1. In age >= 13, substitute 14 for age; 14 is greater than 13, so the result is True.
  2. In attempts != 3, substitute 3 for attempts; 3 is equal to 3, so “not equal to” is not satisfied and the result is False.
  3. In username == "sam", compare the two strings exactly; both are "sam", so the result is True.

The six relational operators you need

For Pearson Edexcel GCSE Computer Science, you need to be able to write programs using all six of these relational operators.

English meaningPython 3 operatorTrue when...Example that is true
equal to==both values are the same7 == 7
less than<the left value is smaller4 < 9
greater than>the left value is larger12 > 5
not equal to!=the values are different6 != 10
less than or equal to<=the left value is smaller or the same10 <= 10
greater than or equal to>=the left value is larger or the same18 >= 16
Key Idea

Comparison gives a Boolean

Every relational expression produces True or False, so it can be used wherever a program needs to make a decision.

Translating English into operators

Some common wording in questions maps neatly to operators:

  • “exactly”, “is equal to” → ==
  • “different from”, “is not” → !=
  • “less than”, “under” → <
  • “more than”, “over” → >
  • “at most”, “no more than”, “up to and including” → <=
  • “at least”, “minimum”, “or more” → >=

For example, “the user must be at least 13” means age >= 13, not age > 13, because 13 itself is allowed.

Equality is not assignment

In Python, assignment means storing a value in a variable. Assignment uses one equals sign:

score = 50

Testing whether two values are equal uses two equals signs:

score == 50
Common Mistake

Using = when you mean ==

Inside a condition, use == to compare values. Writing if score = 50: is invalid Python because = is for assignment, not equality testing.

Correct equality check:

pin = input("Enter PIN: ")

if pin == "2468":
    print("Access granted")
else:
    print("Access denied")

Here, pin == "2468" is the Boolean condition. It will be either True or False.

Using relational operators in selection

Selection is when a program chooses which path to take. In Python, selection usually uses if, elif and else.

A condition is an expression that is tested by if or while. It must evaluate to True or False.

mark = int(input("Enter mark: "))

if mark >= 80:
    print("Distinction")
elif mark >= 50:
    print("Pass")
else:
    print("Fail")

elif means “else if”. Python only checks an elif if the previous if condition was False.

Example

Classifying a mark

Suppose the user enters 72.

  1. Python first tests mark >= 80; substituting 72 gives 72 >= 80, which is False, so the distinction branch is skipped.
  2. Python then tests mark >= 50; substituting 72 gives 72 >= 50, which is True, so Pass is printed.
  3. Because a matching branch has been found, Python does not run the else branch.

The order of tests matters. If you test mark >= 50 before mark >= 80, then a mark of 90 would be accepted as a pass before the program ever reaches the distinction test.

Boundary values: choosing < or <=

A boundary value is a value at the edge of a range, such as 0 or 100 for a percentage mark.

An inclusive boundary includes the edge value. Use <= or >=.

An exclusive boundary does not include the edge value. Use < or >.

For example, if valid scores are from 0 to 100 inclusive, then both 0 and 100 are allowed.

score = int(input("Enter score: "))

if score >= 0 and score <= 100:
    print("Valid score")
else:
    print("Invalid score")

The word and is Python’s version of the logical operator AND. It means both comparisons must be True.

Example

Checking range boundaries

Use the condition score >= 0 and score <= 100.

  1. If score is 100, then score >= 0 is True and score <= 100 is also True, so the whole condition is True.
  2. If score is 101, then score >= 0 is True but score <= 100 is False, so the whole condition is False.
  3. This shows why <= 100 is correct: the boundary value 100 is valid, but 101 is not.

Using relational operators in iteration

Iteration means repeating code. A while loop repeats while its condition is True.

The != operator is especially useful when a program should keep going until a particular value is entered.

choice = input("Enter A to add or Q to quit: ")

while choice != "Q":
    print("Adding item")
    choice = input("Enter A to add or Q to quit: ")
Tip

Think of while as while true

A while loop keeps repeating while the condition is True, so make sure something inside the loop can eventually make the condition False.

Example

Tracing a loop condition

Suppose the user enters A first, then Q.

  1. After the first input, choice is "A", so choice != "Q" is True; the loop body runs and Adding item is printed.
  2. The program asks again, and the user enters "Q", so choice becomes "Q".
  3. Python tests choice != "Q" again; this time it is False, so the loop stops.

Data types matter when comparing input

In Python, input() always returns a string. If you want to do a numerical comparison, convert the input first.

age_text = input("Enter your age: ")
age = int(age_text)

if age >= 18:
    print("Adult")
else:
    print("Under 18")
Common Mistake

Comparing input as text

Do not compare raw input with a number, such as age_text >= 18. Convert it using int() first, then compare the integer value.

String comparisons are fine when you are checking exact text, such as a menu option, username or PIN. Remember that strings are case-sensitive: "Q" and "q" are different values.

Comparing calculated values

Each side of a relational operator can be a calculation, not just a single variable.

correct_answers = int(input("Correct answers: "))
marks = correct_answers * 2

if marks >= 10:
    print("Pass")
else:
    print("Try again")

Python calculates marks first, then compares it with 10. The comparison marks >= 10 still produces a Boolean result.

Exam technique

In the exam

  1. Check whether the boundary value should be included: use < or > if it is excluded, and <= or >= if it is included.
  2. In Python code, use == for equality testing and = only for assignment.
  3. When tracing code, substitute the current variable values into each comparison and decide whether it is True or False.
Self review

Check yourself

  • What is the Python operator for “not equal to”?
  • Why would age > 16 be wrong if the rule says “age 16 and over”?
  • What will happen in a while loop if its condition never becomes False?

Operators

Guide 2 of 3

You've reached the end

Test yourself on this topic, or move on to the next guide.

Next guideUsing logical operatorsStart

How was this guide?

Using relational operators Revision Guide

  1. GCSE
  2. /Computer Science
  3. /Using relational operators