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
False. For this reason, you will sometimes hear them called comparators.
The two boolean operators we’ll cover first are:
These operators compare two items and return
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.
>>> is the prompt when you run Python in your terminal, which you can then use to evaluate simple expressions, such as these.
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.
Determine if the following boolean expressions are
False. Input your answer as
False in the appropriate variable to the right.
(5 * 2) - 1 == 8 + 1
13 - 6 != (3 * 2) + 1
3 * (2 - 1) == 4 - 1