Lesson 11: Boolean Logic¶
In this lesson we will learn
- what Boolean values are
- how comparison operators give back
TrueorFalse - how to use the Boolean operators
not,andandor - how to join comparisons to build more powerful conditions
Terminology
- Boolean operator – an operator that works with
TrueandFalsevalues and always gives backTrueorFalse. - not operator – a Boolean operator that flips a value, so
not TrueisFalseandnot FalseisTrue. - and operator – a Boolean operator that gives back
Trueonly if every value isTrue. - or operator – a Boolean operator that gives back
Trueif at least one value isTrue.
Booleans¶
In programming, Boolean means working with two possible values: True and False.
- a Boolean variable can only store
TrueorFalse - comparison operators (
==,!=,>,<,>=,<=) check something and give backTrueorFalse - Boolean operators (we'll learn these soon) also give back
TrueorFalse
True and False are special words in Python. When we type them in Thonny, they're coloured differently to show they're special.
Comparison operators¶
The conditions in if and while statements check whether something is True or False, using comparison operators. Let's quickly review them. Create a new file, type in the code below, and save it as boolean_logic.py.
PRIMM
- Predict the six results. Each one will be either
TrueorFalse. - Run the code. Were your predictions correct?
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → prints whether
"jeff"is equal to"jeff". - line 2 → prints whether
1is not equal to1. - line 3 → prints whether
500is greater than300. - line 4 → prints whether
100is greater than or equal to250. - line 5 → prints whether
"a"is less than"q". For letters, "less than" means "comes earlier in the alphabet". - line 6 → prints whether
-30is less than or equal to3.
PRIMM
Time to modify the code. Can you change the values so each result switches: every True becomes False, and every False becomes True?
Comparisons work the same way whether the values are typed straight into the code or stored in variables. Change our code so it matches the code below.
PRIMM
- Predict whether the code will print
TrueorFalse. - Run the code. Was your prediction correct?
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → stores
10inscore. - line 2 → prints whether the value in
scoreis greater than5.
Boolean operators¶
Boolean operators work a bit like maths, but instead of numbers they use True and False, and the result is always True or False. They're useful when we want to check more than one thing at the same time. There are three: not, and and or.
The not operator¶
The easiest operator is not. It flips the value: not True is False, and not False is True. Change our code so it matches the code below.
PRIMM
- Predict what the Shell will show.
- Run the code. Was your prediction correct?
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → prints
not True is:followed by the result ofnot True, which isFalse. - line 2 → prints
not False is:followed by the result ofnot False, which isTrue.
The and operator¶
The and operator only gives back True if every value is True. Change our code so it matches the code below.
PRIMM
- Predict what the Shell will show.
- Run the code. Were your predictions correct?
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → prints
True, because both values areTrue. - line 2 → prints
False, because one value isFalse. - line 3 → prints
False, because one value isFalse. - line 4 → prints
False, because both values areFalse. - line 5 → prints
True, because all three values areTrue. - line 6 → prints
False, because one of the three values isFalse.
The or operator¶
The or operator works the opposite way to and. It gives back True if at least one value is True. Change our code so it matches the code below.
PRIMM
- Predict what the Shell will show.
- Run the code. Were your predictions correct?
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → prints
True, because at least one value (in fact both) isTrue. - line 2 → prints
True, because one value isTrue. - line 3 → prints
True, because one value isTrue. - line 4 → prints
False, because no values areTrue. - line 5 → prints
True, because all three values areTrue. - line 6 → prints
True, because one of the three values isTrue.
Joining comparisons¶
Using True and False on their own isn't very useful. But remember, comparison operators give back True or False. So we can use Boolean operators to join comparisons together, and build more powerful conditions for our if and while statements. Change our code so it matches the code below.
PRIMM
- Predict what the Shell will show.
- Run the code. Was your prediction correct?
- Time to investigate the code. What does each line do?
Code explanation
- line 1 → works out both comparisons first (
7 < 8isTrue, and"a" < "o"isTrue), then works outTrue and True, and prints the result,True.
Comparisons on both sides
When we join comparisons, we need a full comparison on both sides of the Boolean operator.
score > 5 and score < 13is correct: both sides are comparisons.score > 5 and < 13is aSyntaxError, because the second part isn't a full comparison.score == 5 or score == 13checks whetherscoreis 5 or 13. Writingscore == 5 or 13runs, but doesn't do what we expect: anifstatement using it always runs its code, because Python treats13on its own asTrue.
Exercises¶
In this course, the exercises are the make part of PRIMM. Work through them to make your own code.
Starter files are in the lesson_11 folder of the tutorial files. Solutions are on the Exercise Solutions page.
Exercise 1¶
Starter: lesson_11/ex1_all_true
Each line in this program prints False. Can you change the values so every line prints True? Only change the numbers and the Boolean values.
Exercise 2¶
Starter: lesson_11/ex2_pass_mark
Can you write a program that asks the user for their test score out of 100, then prints True if the score is a pass (from 50 to 100), and False if it isn't? Use and to join two comparisons.
Exercise 3¶
Starter: lesson_11/ex3_weekend
Can you write a program that asks the user what day it is, then prints True if it's Saturday or Sunday, and False if it isn't? Use or to join two comparisons.