Relational and Boolean Operators
beginner25 minLearning objectives
- Use relational operators to compare values
- Construct Boolean expressions using logical operators
- Evaluate truth tables
- Apply relational and Boolean operators to solve programming problems
Learn
AQA 4.1.1.4–4.1.1.5 — Relational and Boolean operators
Retrieval: before this lesson, you should be able to evaluate arithmetic expressions (+ - * / // % **) and explain operator precedence from the previous lesson — relational operators reuse that same idea of evaluating an expression to produce a value, except the value produced is always True or False.
Relational operators
A relational operator compares two values and always evaluates to a Boolean.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | equal to | 5 == 5 | True |
!= | not equal to | 5 != 3 | True |
< | less than | 3 < 5 | True |
> | greater than | 3 > 5 | False |
<= | less than or equal to | 5 <= 5 | True |
>= | greater than or equal to | 4 >= 5 | False |
Common mistake
= and == are not interchangeable: = assigns a value (age = 17), == compares two values (age == 17). Writing if age = 17: is a syntax error in Python — but in some other languages it silently does something you didn't intend, so it's worth being precise about the distinction rather than relying on Python to catch it for you.
Boolean operators and truth tables
Boolean operators combine one or more Boolean values into a single Boolean result.
| A | B | A and B | A or B | not A | A xor B |
|---|---|---|---|---|---|
| True | True | True | True | False | False |
| True | False | False | True | False | True |
| False | True | False | True | True | True |
| False | False | False | False | True | False |
Python has no built-in xor keyword for Booleans — it's usually written as A != B (true exactly when they differ), which is worth recognising since AQA's specification still treats XOR as one of the four core Boolean operators.
age = 17
has_id = True
can_enter = age >= 18 and has_id
print(can_enter) # False - age >= 18 is False, so the AND short-circuits to False
Worked example — a password validator
password = input("Enter a password: ")
min_length = 8
is_long_enough = len(password) >= min_length
has_digit = any(c.isdigit() for c in password)
if is_long_enough and has_digit:
print("Password accepted.")
else:
print("Password rejected.")
Combining two independent Boolean checks with and means both conditions must be true — this is the same relational-plus-Boolean pattern you'll use constantly for input validation throughout the course.
Challenge
Extend the password validator above so it also requires at least one uppercase letter, using any(c.isupper() for c in password), and prints which specific rule failed rather than a single generic "rejected" message.
Looking ahead: the next sequence uses exactly this relational/Boolean toolkit to build selection (if/elif/else) and, later, the loop conditions that control iteration — so being fluent with these operators now pays off immediately.