Now that we understand what boolean expressions are, let’s learn to create them in Python. We can create a boolean expression by using relational operators.
Relational operators compare two items and return either True
or False
. For this reason, you will sometimes hear them called comparators.
The two relational operators we’ll cover first are:
- Equals:
==
- Not equals:
!=
These operators compare two items and return True
or False
if they are equal or not.
We can create boolean expressions by comparing two values using these operators:
1 == 1 # True 2 != 4 # True 3 == 5 # False '7' == 7 # False
Each of these is an example of a boolean expression.
Why is the last statement false? The ''
marks in '7'
make it a string, which is different from the integer value 7
, so they are not equal. When using relational operators it is important to always be mindful of type.
Instructions
Determine if the following boolean expressions are True
or False
. Input your answer as True
or False
in the appropriate variable to the right.
Statement one:
(5 * 2) - 1 == 8 + 1
Statement two:
13 - 6 != (3 * 2) + 1
Statement three:
3 * (2 - 1) == 4 - 1