- 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.
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.
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?”
Evaluating simple comparisons
Suppose age = 14, attempts = 3 and username = "sam".
- In
age >= 13, substitute 14 for age; 14 is greater than 13, so the result is True.
- In
attempts != 3, substitute 3 for attempts; 3 is equal to 3, so “not equal to” is not satisfied and the result is False.
- In
username == "sam", compare the two strings exactly; both are "sam", so the result is True.
For Pearson Edexcel GCSE Computer Science, you need to be able to write programs using all six of these relational operators.
| English meaning | Python 3 operator | True when... | Example that is true |
|---|
| equal to | == | both values are the same | 7 == 7 |
| less than | < | the left value is smaller | 4 < 9 |
| greater than | > | the left value is larger | 12 > 5 |
| not equal to | != | the values are different | 6 != 10 |
| less than or equal to | <= | the left value is smaller or the same | 10 <= 10 |
| greater than or equal to | >= | the left value is larger or the same | 18 >= 16 |
Comparison gives a Boolean
Every relational expression produces True or False, so it can be used wherever a program needs to make a decision.
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.
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
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.
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.
Classifying a mark
Suppose the user enters 72.
- Python first tests
mark >= 80; substituting 72 gives 72 >= 80, which is False, so the distinction branch is skipped.
- Python then tests
mark >= 50; substituting 72 gives 72 >= 50, which is True, so Pass is printed.
- 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.
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.
Checking range boundaries
Use the condition score >= 0 and score <= 100.
- If
score is 100, then score >= 0 is True and score <= 100 is also True, so the whole condition is True.
- If
score is 101, then score >= 0 is True but score <= 100 is False, so the whole condition is False.
- This shows why
<= 100 is correct: the boundary value 100 is valid, but 101 is not.
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: ")
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.
Tracing a loop condition
Suppose the user enters A first, then Q.
- After the first input,
choice is "A", so choice != "Q" is True; the loop body runs and Adding item is printed.
- The program asks again, and the user enters
"Q", so choice becomes "Q".
- Python tests
choice != "Q" again; this time it is False, so the loop stops.
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")
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.
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.
In the exam
- Check whether the boundary value should be included: use
< or > if it is excluded, and <= or >= if it is included.
- In Python code, use
== for equality testing and = only for assignment.
- When tracing code, substitute the current variable values into each comparison and decide whether it is
True or False.
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?