5.7. Truth Tables¶
Truth tables help us understand how logical operators work by showing all
of the possible return values. Let’s look at the truth table for and
, which
assumes we have two boolean expressions, A
and B
.
Example
A 
B 
A 













Consider the first row of the table. This row states that if A is true
and B is true, then A and B
is true. The two middle rows show that if
either A or B is false, then A and B
is false. Finally, if both A and B are
false, then A and B
is false.
Try It!
Now take a look at the truth table for or
.
PREDICT whether
A or B
should beTrue
orFalse
for each row.Click in the empty spaces to check your answers.
A 
B 
A 



True 


True 


True 


False 
5.7.1. Order of Operations¶
We now have a lot of operators in our toolkit, so it is important to understand how they relate to each other. Which operators get done first?
Python always performs operations in a specific order:
It does all math calculations first.
Next, it evaluates all comparisons as
True
orFalse
.Next, it applies all
not
operators.Finally, it evaluates
and
andor
operations.
Example
The expression x * 5 >= 10 and y  6 <= 20
will be completed in this order:
x * 5
is calculated, theny  6
.The
>=
comparison is evaluated asTrue
orFalse
.The
<=
comparison is evaluated asTrue
orFalse
.The
and
operator is evaluated last.
Let’s say x = 2
and y = 46
. Here we step through each stage of the evaluation:
Action 
Result 

Plug in the values into the expression 



The 

The 

The 

5.7.1.1. Table of Operator Order¶
The following table lists operators in order of importance, from highest (applied first) to lowest (applied last).
Level 
Category 
Operators 

(Highest) 
Parentheses 

Exponent 


Multiplication and Division 


Addition and subtraction 


Comparison 


Logical 


Logical 


(Lowest) 
Logical 

Tip
Using parentheses is not always necessary, but they make a BIG difference when someone else reads your code. As a best practice, use parentheses to make your code easier to read:
x * 5 >= 10 and y  6 <= 20
vs.
(x * 5 >= 10) and (y  6 <= 20)
5.7.2. Check Your Understanding¶
Question
Assume we have 3 boolean expressions (A, B, and C). Which combinations of
values (A/B/C) will make the expression A or B and C
evaluate to
True
? Click ALL that apply.
 True / True / True
 False / True / True
 True / False / True
 True / True / False
 False / False / True
 False / True / False
 True / False / False
 False / False / False