Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

2.4 (2 of 3): Comparing and combining with and, or, not

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

The person at the cinema door

A worker at the cinema door checks you. Are you 15 or older? Do you have a ticket? You can go in only if both answers are yes.

Each question has a yes or no answer: True or False. A program asks the same questions with two kinds of operator. An operator is a symbol or word that does something to values.

Key words

WordWhat it meansExample
Relational operatorAn operator that compares two values and gives True or False.age >= 15
BooleanA value that is True or False.True
Boolean operatorAn operator that joins True and False values: and, or, not.a and b
OperatorA symbol or word that does something to values.>
Truth tableA table that shows the result of a Boolean operator for every case.True and False is False
ConditionA question that gives True or False.score > 10

The cells students fill in are left blank in this preview.

Watch: asking and joining questions

Press Play on the short film below. It asks questions with < and !=, shows why one = cannot ask one, then joins the answers with and, or and not.

Film: comparing and combining with and, or, not

Comparing and combining: and, or, not

Every scene, as text

  1. A relational operator compares two values and gives True or False. With age set to 16, print(age < 16) shows False, as 16 is not less than 16, and print(age != 20) shows True, as 16 is not equal to 20.
  2. Myth (false): one = asks if two values are equal. One = puts a value into a name, so Python cannot read if age = 16: as a question and stops with a SyntaxError before any line runs. Python suggests ==, the one you want here.
  3. Two == ask a question: is age equal to 16? In a condition, write if age == 16: the answer is True, so the indented print(age) runs and shows 16.
  4. and needs both sides to be True, like the cinema door: 15 or older and a ticket. With age set to 14 and has_ticket set to True, print(age >= 15 and has_ticket) shows False, as age >= 15 is False.
  5. or needs at least one side to be True: pay by cash or by card. With has_cash set to False and has_card set to True, print(has_cash or has_card) shows True.
  6. not turns True into False and False into True. With locked set to False, print(not locked) shows True: the door is not locked.

Try it: compare

Code: python
age = 16

print(age == 16)
print(age < 16)
print(age >= 16)
print(age != 20)

# Each line asks a question. The answer is True or False.
# Change age to 15 and run again.

Make a free account to save your work and track your progress.

The six relational operators

OperatorMeansExampleResult when age is 16
`==`Is equal to`age == 16`True
`!=`Is not equal to`age != 16`False
`<`Is less than`age < 16`False
`>`Is greater than`age > 16`False
`<=`Is less than or equal to`age <= 16`True
`>=`Is greater than or equal to`age >= 16`True

The cells students fill in are left blank in this preview.

One equals or two?

Warning

Many people write if age = 16. One = puts a value into a name. Two asks a question: are these equal? In a condition, write if age 16.

True or False?

Have a go

True or False?

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Join questions with and, or, not

and needs both sides to be True. or needs at least one side to be True. not turns True into False and False into True.

A truth table shows every case.

Truth table for and, or

ABA and BA or B
TrueTrueTrueTrue
TrueFalseFalseTrue
FalseTrueFalseTrue
FalseFalseFalseFalse

The cells students fill in are left blank in this preview.

Truth table for not

Anot A
TrueFalse
FalseTrue

The cells students fill in are left blank in this preview.

Match the operator to its job

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Try it: and, or, not

Code: python
age = 16
has_ticket = True

print(age >= 15 and has_ticket)
print(age < 12 or has_ticket)
print(not has_ticket)

# Change has_ticket to False and run again.
# Which lines change?

Make a free account to save your work and track your progress.

Work out the answer

Have a go

Work out the answer

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Brackets make the order clear

Note

When you join several questions, use brackets so the order is clear. (a or b) and c is clear to every reader.

Extra: Try it: the order without brackets

Code: python
is_member = True
has_coupon = True
age = 12

print(is_member or has_coupon and age >= 15)
print((is_member or has_coupon) and age >= 15)

# Python works out comparisons first, then not, then and, then or.
# So the first line means: is_member or (has_coupon and age >= 15).
# The two lines give different answers. Brackets make it clear.

Make a free account to save your work and track your progress.

Your turn: four tests

Write four conditions. can_enter is True when the age is 15 or more and the person has a ticket. is_weekend is True for the text Sat or Sun. needs_coat is True when it is raining or it is cold. is_not_late is True when the minutes late is 0. Press Run Tests.

Code: python
def can_enter(age, has_ticket):
    # You can enter if you are 15 or older AND you have a ticket.
    return None


def is_weekend(day):
    # day is text such as "Sat". True for "Sat" or "Sun".
    return None


def needs_coat(raining, cold):
    # You need a coat if it is raining OR it is cold.
    return None


def is_not_late(minutes_late):
    # On time means 0 minutes late. Use not, or a comparison.
    return None

Make a free account to save your work, or press Start lab to work through the whole lesson.

Key words crossword

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Exit quiz

Have a go

Exit quiz

6 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

Important

Relational: == != < > <= >=. Boolean: and, or, not. and needs both True. or needs one True. Use == to compare, never a single =.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.