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
| Word | What it means | Example |
|---|---|---|
| Relational operator | An operator that compares two values and gives True or False. | age >= 15 |
| Boolean | A value that is True or False. | True |
| Boolean operator | An operator that joins True and False values: and, or, not. | a and b |
| Operator | A symbol or word that does something to values. | > |
| Truth table | A table that shows the result of a Boolean operator for every case. | True and False is False |
| Condition | A 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
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
| Operator | Means | Example | Result 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 questionsJoin 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
| A | B | A and B | A or B |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | True | False | True |
| False | False | False | False |
The cells students fill in are left blank in this preview.
Truth table for not
| A | not A |
|---|---|
| True | False |
| False | True |
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
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 questionsBrackets 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
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.
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 questionsRemember
Important
Relational: == != < > <= >=. Boolean: and, or, not. and needs both True. or needs one True. Use == to compare, never a single =.