Relational and Boolean Operators

beginner25 min

Learning 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.

OperatorMeaningExampleResult
==equal to5 == 5True
!=not equal to5 != 3True
<less than3 < 5True
>greater than3 > 5False
<=less than or equal to5 <= 5True
>=greater than or equal to4 >= 5False

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.

ABA and BA or Bnot AA xor B
TrueTrueTrueTrueFalseFalse
TrueFalseFalseTrueFalseTrue
FalseTrueFalseTrueTrueTrue
FalseFalseFalseFalseTrueFalse

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.

Practise

Apply what you've just learned in the Coding Lab.

Open Coding Lab

Test yourself

Check your understanding with exam-style questions.

Go to Exam Practice
Log in to track this lesson on your progress dashboard.
Log in