GCSE · Computer Science · Edexcel · Spec 1CP2
Programming logical operators
The 10 of hearts and the 10 of spades are almost twins, yet one program test lets the hearts card in and turns the spades card away.
Computer Science · Logical operators
Two comparisons, one answer
Let A stand for age > 12 and B stand for age < 60. Each row is one way those two comparisons can turn out. The last three columns show what and, or and not make of them.
Step through the four rows. Before you read each note, look at the highlighted row and say what you expect from and, or and not.
| A | B | A and B | A or B | not A |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
Output
Step 1: Both comparisons come out True, like someone aged 30. and is True because both are. or is True too: it only needs either one, and both still counts. This is the row people doubt. not flips A from True to False.
When you have stepped through all four, run your eye down each column. and is True in just one row. or is False in just one row.
Computer Science · Logical operators
Same cards, different rule
Each card has a value from 1 to 13 and a suit. Pick a card, then put it under True or False for the rule shown. When all seven are sorted, switch the rule and sort the same cards again.
Does the card make value <= 10 and suit == "hearts" come out True?
Still to sort
True (0)
The whole condition comes out True.
Where the line is: Judge the whole condition, after its comparisons have been combined.
False (0)
The whole condition comes out False.
Where the line is: Judge the whole condition, after its comparisons have been combined.
Computer Science · Logical operators
Which branch does the card take?
Pick a selection statement, then pick a card, and watch which branch runs.
Which selection statement? → Which card?
4 possible routes.
Whichever card you pick, the program takes exactly one route: the if branch or the else branch. In the second program a picture card’s value is stored as a word, like "king".
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Combine several comparisons into one True or False answer.
What you need to know
- One comparison isn't always enough. Logical operators combine several comparisons into a single True or False.
- There are three basic logical operators: and, or and not.
- and is True only when both comparisons are True.
- or is True when either comparison is True, or both are.
Have a goA classmate insists: "or can't be True when both comparisons are True. That would be cheating." Is 7 > 3 or 7 < 10 True or False?
True.
Both comparisons are True, and or accepts either one or both. Your classmate is treating or as "pick exactly one".
- not flips a comparison: it is True when the comparison it is applied to is False.
- A logical expression mixes relational and logical operators, like age > 12 and age < 60. Each side is its own comparison.
Have a goA site gives a discount when the visitor's age is under 5 or over 65. Write that as a logical expression using age.
age < 5 or age > 65
Each side is a full comparison with age, and or joins them because either one is enough.
- An arithmetic expression such as 30 + 3 gives a number. A logical expression gives True or False.
- Logical expressions can be the condition in an if statement, with elif and else choosing between options.
- In Python the logical operators are written in lowercase: and, not And.
Have a goA friend types if age > 12 And age < 60: and Python complains. What do you tell them?
Write and in lowercase: if age > 12 and age < 60:
Python's logical operators are lowercase, so And with a capital A isn't the operator.
- A comparison inside a condition needs ==, not the single = used for assignment.
The big picture
When one comparison isn't enough, logical operators combine several relational expressions into one True or False. The three basic ones are and (True when both are True), or (True when either or both are) and not (True when its expression is False). A logical expression, such as age > 12 and age < 60, evaluates to True or False, whereas an arithmetic expression such as 30 + 3 gives a number. Logical expressions can be the condition in an if statement, with elif and else choosing between options. In Python they are lowercase, and a comparison in a condition needs ==, not =.
Key points
Worked example
Problem
adult_ticket = age > 12 and age < 60. Work out the value stored in adult_ticket when age is 60.
⚠ Watch out
Reading or as "one or the other, but not both" and answering False when both comparisons are True. or is True when either or both are True. A second common slip is a single = instead of == inside a condition.
Memory hook
and wants both, or settles for either (or both), and not flips the verdict.
Check yourself
Take value > 7 and suit == "hearts". Find a card that makes the and version True, then one that makes the and version False but the or version True.
Flashcards
(10)Why would a program use a logical operator?
Name the three basic logical operators.
When is each of and, or and not True?
What is a logical expression?
What does an arithmetic expression evaluate to, and what does a logical one evaluate to?
With score = 20, what does score > 30 evaluate to?
Test the 10 of hearts and the 10 of spades with value <= 10 and suit == "hearts". What does each give?
How are the logical operators written in Python?
In a condition, should you compare with = or ==?
Which three keywords let a selection statement choose between several options?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
- Programming – condition-controlled repetition
- Programming – count-controlled repetition
- Programming – iteration over data structures
- Programming – selection
- Programming – sequencing
- Programming arithmetic operators
- Programming relational operators
- Reading, writing, analysing and refining programs
- Single entry/exit code blocks
- String manipulation
- Structural components of programs
- Structured data types
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 12 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.