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.
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.
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?
data-callouts.warning.label
One = puts a value into a name. Two == asks a question: are these equal? Writing if age = 16 is a mistake. Write if age == 16.
True or False?
Have a go
True or False?
4 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.
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
| 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.