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.

ABA and BA or Bnot A
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue
A=True, B=True, A and B=True, A or B=True → not A = False

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.

1 / 4

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.

Step 1 of 4: 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..

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.

7 of 7 still to sort.

Two kinds of expression

Arithmetic expressionvsLogical expression

Look at what each one gives you back.

Focus

What it evaluates to

Arithmetic expression

A number

Logical expression

True or False

The insight

This is the whole difference. Ask what you get back: a number, or True or False.

Example

Arithmetic expression

number = 30 + 3 gives 33

Logical expression

With score = 20, score > 30 evaluates to False

What it is built from

Arithmetic expression

Arithmetic operators, like the + in 30 + 3

Logical expression

Relational operators like > and <, joined by and, or and not when more values need comparing

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.

On A card goes into a selection statement. 2 branches to choose from.

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

Computer Science · Logical operators

One line stops this program running

This program prices an order by item and filling. Python refuses to run it because one line is wrong. Which line is it?

A student's program — which line goes wrong?

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

1Logical operators combine several comparisons into one True or False. The three basic ones are and, or and not.
2and is True when both comparisons are True. or is True when either or both are. not is True when its comparison is False.
3A logical expression combines relational and logical operators and evaluates to True or False. An arithmetic expression evaluates to a number.
4A logical expression can be the condition of an if statement, and elif and else choose between several options.
5In Python write the operators in lowercase (and, or, not), and use == for comparing, not the single = of assignment.

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?
When one comparison isn't enough. Logical operators combine several comparisons made with relational operators into a single True or False.
Name the three basic logical operators.
and, or and not.
When is each of and, or and not True?
and: when both comparisons are True. or: when either or both are True. not: when the comparison it is applied to is False.
What is a logical expression?
An expression that combines relational and logical operators, such as age > 12 and age < 60. It evaluates to True or False.
What does an arithmetic expression evaluate to, and what does a logical one evaluate to?
An arithmetic expression evaluates to a number (30 + 3 gives 33). A logical expression evaluates to True or False.
With score = 20, what does score > 30 evaluate to?
False, because 20 is not greater than 30.
Test the 10 of hearts and the 10 of spades with value <= 10 and suit == "hearts". What does each give?
The 10 of hearts gives True. The 10 of spades gives False, because suit == "hearts" is False.
How are the logical operators written in Python?
In lowercase: and, or, not. Write and, not And.
In a condition, should you compare with = or ==?
==. The single = is assignment, which stores a value in a variable.
Which three keywords let a selection statement choose between several options?
if, elif and else.

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 posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

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.