- Understand how AND, OR, and NOT extend simple conditions into complex ones
- Use truth tables to evaluate Boolean expressions step by step
- Read complex conditions in SQA reference language and write them in Python
- Apply operator precedence rules when mixing AND, OR, and NOT in one expression
- I can describe what each Boolean operator does and give an example
- I can complete a truth table for an expression using AND, OR, and NOT
- I can trace the result of a complex condition for given variable values
- I can read an IF statement in SQA reference language and write an if statement with a complex condition in Python
- I can explain why brackets are needed when mixing AND and OR
Answer before the lesson begins. These check prior knowledge from selection (SDD7).
1. What does a Boolean expression always evaluate to?
2. In Python, which symbol checks whether two values are equal?
SQA reference language (read only)
SQA uses reference language in question papers. You need to be able to read it. You will never be asked to write it.
3. In SQA reference language, what keyword follows the condition in an IF statement?
Key vocabulary
Complex Conditions: AND, OR, NOT
Why simple conditions aren't always enough
A simple condition compares two values: age >= 16, score > 50. Many real problems need more than one condition checked at the same time. Should a user get a discount if they are under 12 or over 65? Should a door unlock only if the PIN is correct and the user is not suspended? These situations require complex conditions — two or more Boolean expressions joined by the operators AND, OR, or NOT.
The AND operator
AND joins two conditions and returns True only when both conditions are True. If either one is False, the whole expression is False. Think of it as a strict gate: every condition must pass.
| A | B | A AND B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
Example: a ride requires the rider to be at least 10 years old AND at least 130 cm tall. A 12-year-old who is 125 cm tall fails because, although the age condition is True, the height condition is False. AND gives False overall.
The OR operator
OR returns True when at least one of the conditions is True. Only when every condition is False does OR give False. Think of it as a lenient gate: any condition passing is enough.
| A | B | A OR B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
Example: a cinema gives a discount to children under 12 OR senior citizens over 65. A 70-year-old qualifies because the second condition is True even though the first is False.
The NOT operator
NOT works on a single condition and reverses it. NOT True gives False; NOT False gives True. It is used when you want something to be absent or untrue rather than present or true.
| A | NOT A |
|---|---|
| True | False |
| False | True |
Example: a system sends a reminder only if the task is NOT complete. If complete = False, then NOT complete is True and the reminder is sent.
Reading reference language and writing Python
The logic is identical in both languages; only the formatting differs. SQA reference language uses capital AND, OR, NOT as keywords. Python uses the same words but entirely lowercase. Read the reference-language example and compare it with Python; write your own code in Python.
SQA reference language (read only)
IF age >= 12 AND age <= 17 THEN
SEND "Teen price applies." TO DISPLAY
END IF
Python implementation
if age >= 12 and age <= 17:
print("Teen price applies.")
Both examples check that age is between 12 and 17 inclusive. In Python, and must be lowercase — writing AND in Python causes a syntax error.
Operator precedence and brackets
When AND and OR appear together in one expression, NOT is evaluated first, then AND, then OR. This is the same principle as BODMAS in arithmetic. Use brackets to make the intended order explicit and your code easier to read.
SQA reference language (read only)
Read and compare these condition fragments:
# Which operator has priority? IF isStudent OR isStaff AND hasTicket THEN ... # Clear — brackets show exactly what is grouped IF isStudent OR (isStaff AND hasTicket) THEN ...
Without brackets, AND is evaluated before OR, so isStaff AND hasTicket is evaluated first and then OR-ed with isStudent. Adding brackets makes this visible and removes any doubt.
Worked examples
A programme gives a junior discount to anyone aged between 5 and 15 inclusive. Evaluate whether a 10-year-old qualifies.
SQA reference language (read only)
DECLARE age AS INTEGER INITIALLY 0
RECEIVE age FROM (INTEGER) KEYBOARD
IF age >= 5 AND age <= 15 THEN
SEND "Junior discount applied." TO DISPLAY
ELSE
SEND "No junior discount." TO DISPLAY
END IF
10 >= 5 → True10 <= 15 → TrueA leisure centre offers free entry to children (under 5) or senior citizens (over 70). Write the Python code and evaluate for a 3-year-old and a 45-year-old.
age = int(input("Enter age: "))
if age < 5 or age > 70:
print("Free entry.")
else:
print("Standard charge.")
3 < 5 is True. True OR anything → True. Output: "Free entry."45 < 5 is False. 45 > 70 is also False. False OR False → False. Output: "Standard charge."A system should display a reminder only when a task is not yet complete. The variable complete stores True or False.
SQA reference language (read only)
IF NOT complete THEN
SEND "Reminder: task still outstanding." TO DISPLAY
END IF
Python implementation
if not complete:
print("Reminder: task still outstanding.")
complete = False: NOT False → True. The reminder is displayed.complete = True: NOT True → False. The condition fails and no reminder appears.complete == False, though both are valid.A streaming service allows access if the user has an active subscription OR (is a new user AND has entered a valid trial code).
hasSubscription = True
isNewUser = False
validCode = True
if hasSubscription or (isNewUser and validCode):
print("Access granted.")
else:
print("Access denied.")
isNewUser AND validCode → False AND True → FalsehasSubscription OR False → True OR False → TrueA theme park ride has the following rules: riders must be aged 10 or over AND at least 130 cm tall. If either requirement is not met, the rider is turned away.
Answer the following:
- Write a Python if/else statement using and that displays "You may ride." if the rider qualifies and "Sorry, you do not meet the requirements." otherwise. Assume age and height already contain integer values.
- Evaluate the condition manually for a rider who is 9 years old and 145 cm tall. Show each step.
- Explain whether a rider aged 10 and exactly 130 cm tall qualifies, and why.
if age >= 10 and height >= 130: print("You may ride.") else: print("Sorry, you do not meet the requirements.")- Step 1:
9 >= 10→ False. Step 2:145 >= 130→ True. Step 3: False AND True → False. The rider is turned away — even though the height is fine, the age requirement fails AND requires both. - The rider qualifies: both minimum requirements are met exactly. Output:
You may ride.. Accept any correct Python solution with the same conditions and outputs.
&& or || instead of AND / OR. Some other languages use && and ||, but SQA reference language uses the words AND and OR. Python requires lowercase and/or; && and || cause syntax errors in Python. Written code answers may use any programming language, so use the correct operators for the language you choose.AND in Python causes a syntax error — the correct Python keywords are and, or, not (all lowercase).A OR B AND C evaluates AND before OR because of precedence. If that is not what you intend, you must use brackets: A OR (B AND C) or (A OR B) AND C.age == 16 or 17. Python does not read this as two age comparisons — it reads 17 as a separate non-zero value, which is always truthy. The correct form is age == 16 or age == 17.SQA reference language (read only)
When asked to evaluate a complex condition, always work through it in steps. Write out each simple condition as True or False first, then apply the operator. For example, if the question gives x = 4 and the condition is x > 2 AND x < 10, write: True AND True → True. Showing your working makes your reasoning easier to check. SQA reference language uses capital AND, OR, NOT; when writing Python, use lowercase and, or, not.
Questions 1–5 are auto-checked. Questions 6–8 are self-marked — write your answer, then reveal the model answer to check your work. Questions 9–10 are practical PyCharm implementation tasks.
SQA reference language (read only)
1. What does score >= 50 AND age >= 16 evaluate to when score = 60 and age = 15? TYPE 1
SQA reference language (read only)
2. Which expression evaluates to True when hasMembership is FALSE? TYPE 1
3. In Python, which of these correctly checks that x is between 1 and 10 inclusive? TYPE 1
SQA reference language (read only)
4. The condition is x > 10 OR x < 0. Which value of x makes this False? TYPE 1
SQA reference language (read only)
5. What is the result of NOT (TRUE AND FALSE)? TYPE 1
6. Write Python code for a door security system. A door should unlock only if the PIN is correct AND the user is not suspended. Assume variables pinCorrect and suspended already contain Boolean values (True or False). Display "Door unlocked." or "Access denied." as appropriate. TYPE 2
if pinCorrect and not suspended:
print("Door unlocked.")
else:
print("Access denied.")
The condition requires both parts to be True: the PIN must be correct and the user must not be suspended. not suspended is True when suspended is False. Only (True, False) unlocks the door; (True, True), (False, False) and (False, True) all display Access denied.. Accept equivalent correct Python code.
SQA reference language (read only)
7. Complete the truth table below for the expression A OR (NOT B). Fill in the missing values for NOT B and the final result. TYPE 2
| A | B | NOT B | A OR (NOT B) |
|---|---|---|---|
| True | True | ? | ? |
| True | False | ? | ? |
| False | True | ? | ? |
| False | False | ? | ? |
| A | B | NOT B | A OR (NOT B) |
|---|---|---|---|
| True | True | False | True |
| True | False | True | True |
| False | True | False | False |
| False | False | True | True |
The only False result is when A is False and B is True, because NOT True = False and False OR False = False.
8. Explain the difference between AND and OR. For each operator, give a realistic example of a situation where you would use it in a program. TYPE 2
AND returns True only when every condition is True. Use it when all requirements must be met at the same time. Example: a bank login requires the username to be correct AND the password to be correct — both must match, not just one.
OR returns True when at least one condition is True. Use it when any one of several alternatives qualifies. Example: a sports club gives a discount to members who are under 16 OR over 60 — satisfying either age group is enough to qualify.
9. Practical: In PyCharm, create a new Python file called sdd8_conditions.py. Write a program that accepts a password from the user. The password is valid only if it is longer than 8 characters AND starts with the letter "P". Display "Password accepted." or "Invalid password." as appropriate. Run your program and test it with: (a) Pverylongpassword — accepted; (b) Pass — too short; (c) longpassword — wrong first letter. Record each output. TYPE 3
password = input("Enter password: ")
if len(password) > 8 and password[0] == "P":
print("Password accepted.")
else:
print("Invalid password.")
Test results:
(a) Pverylongpassword (len 17, starts P) → Password accepted.
(b) Pass (len 4, too short) → Invalid password.
(c) longpassword (len 12, starts l not P) → Invalid password.
len(password) > 8 checks the length. password[0] == "P" checks the first character (index 0). Both must be True — hence AND.
10. Practical: In sdd8_conditions.py, write a second Python program that checks whether a visitor can board a theme park attraction. The rules are: the visitor must be at least 120 cm tall AND must not have a heart condition. Ask the user for their height in cm (integer) and whether they have a heart condition ("yes" or "no"). Display "You may ride." or "Entry refused.". Test with these three scenarios and record each output: (a) height 130, no heart condition; (b) height 110, no heart condition; (c) height 130, heart condition. TYPE 3
height = int(input("Enter height in cm: "))
heart = input("Heart condition? (yes/no): ").lower()
if height >= 120 and not (heart == "yes"):
print("You may ride.")
else:
print("Entry refused.")
Test results:
(a) 130 cm, "no" → You may ride. (height ≥ 120 AND NOT False = True AND True)
(b) 110 cm, "no" → Entry refused. (height < 120, first condition False)
(c) 130 cm, "yes" → Entry refused. (NOT True = False, second condition False)
not (heart == "yes") applies the NOT operator taught in this lesson. .lower() ensures "Yes" and "YES" are also handled correctly.
Suggested timing: 65 minutes. Warm up 5 min; notes and truth table walkthrough 20 min; worked examples 10 min; now you try 5 min; task set 25 min.
Key misconception to address: AND does not mean "add both things together" — it means both must be True simultaneously. Many pupils assume OR is more restrictive than AND; in fact OR is more permissive.
Live demo suggestion: Start with two simple conditions on the board (e.g., age and membership). Ask pupils to decide AND or OR for different scenarios before any code is shown. This builds intuition before syntax.
Common pitfall to watch: In Python, x == 16 or 17 does not behave as expected — 17 is always truthy. Demonstrate this live so pupils see the bug before writing it in their own code.
Extension question: Ask all pupils to trace what happens when you have three conditions joined by AND and OR, and to draw a truth table with all 8 rows (2³ combinations).
Notation and self-marking: The bridge and Q6 require Python code. Check the intended logic and accept equivalent correct Python solutions; for Q6 only a correct PIN with no suspension unlocks the door. Worked reference-language examples and labelled tasks are for reading and tracing.
SQA command words covered: describe, explain, evaluate, write, complete (truth table).