Home » Chapter 3 : Decisions
Booleans & Conditions
Boolean is a binary state that can only be one of two values, True or False. Boolean variables in Python can only hold one of those two values and conditions can only result in one of those two values. We use boolean in many scenarios in programming, such as repeating an action until some condition becomes True. We often associate boolean result values of True and False to Yes or No questions.
Here are some example questions and statements that we could implement in Python that would rely on boolean values:
On the surface this sounds easy, it's just Yes (True) or No (False). However, there are many nuances about "truth" in programming. So, we need to learn what True & False mean in Python, how we represent those values, how we evaluate those values, and how we handle those values in our code. We will get started here, and like many things in programming, we will keep adding to this idea as we proceed.
First let's remind ourselves about the Logical Operators as we'll need these in our discussion.
if user == employed:
# do something
This code example demonstrates the use of the bool variable in a decision statement condition. We can use it in this way because the variable employed will contain either only True or False.
The simplest truth table is a single variable that displays all possible boolean values for the variable, which are only True and False.
x |
---|
True |
False |
Since our first truth table contains only one variable, the only operation we can perform on it using the logical operators we know is not. The not operator reverses the truth value of a variable or expression. The second column now displays the result of using not on the variable.
x | not x |
---|---|
True | False |
False | True |
When we add a second variable to our truth table, we add the variable to its column in the table and enter its possible values given the problem scenario.
x | y |
---|---|
True | True |
True | False |
False | True |
False | False |
Now that we have two variables, we can use the other logical operators and and or. With our propositions and value columns set up, we add columns for each comparison we may need in the problem solution. In this example, we want to evaluate x and y and x or y. Then for each row, we apply the logical operators to the propositions in the first two columns and populate the appropriate cells in the comparison columns. For example, on the first row x = True and y = True, so we evaluate the comparision of x and y, that is, True and True, the result of which is True. So we enter True in the first row under x and y. We then continue with the remaining cells to fill out the rest of the table.
x | y | x and y | x or y |
---|---|---|---|
True | True | True | |
True | False | ||
False | True | ||
False | False |
Once the table is fully populated we can use this table to prepare a code solution that will cover all possible results from the compound condition problem. So, we can transfer these logical results to a programming construct so that, for example, all four possible outcomes of x and y have a coded action as needed.
x | y | x and y | x or y |
---|---|---|---|
True | True | True | True |
True | False | False | True |
False | True | False | True |
False | False | False | True |
We are not limited to using just two variables in a truth table. Our problem scenario may require more than two boolean variables to evaluate. In these cases, truth tables are very useful to help us visualize the possible outcomes.
Based on this truth table, answer the following question:
w | x | y | z | w and x and y and z |
---|---|---|---|---|
True | False | True | True | False |
True | True | True | True | True |
True | True | False | True | False |
False | True | False | True | False |
Explain the results displayed in the fifth column for this truth table where w and x and y and z.
Also, we are not limited to using just one logical operator at a time. Our problem scenario may require compound conditions as well. Compound conditions are several conditions that, when combined, return a single True/False result. In these cases, truth tables are very useful to help us visualize the possible outcomes. For example, consider this truth table:
Based on this truth table, answer the following question:
w | x | y | z | w and (x or y) and (z or w) |
---|---|---|---|---|
True | False | True | True | True |
True | True | True | True | True |
True | True | False | True | True |
False | True | False | True | False |
Explain the results displayed in the fifth column for this truth table where w and (x or y) and (z or w).
In Python, boolean values are numeric and therefore we can use the comparison operators with them as well as logical operators. Consider the following code:
x = True
y = False
print(x > y)
print(y > x)
What do you suspect will be printed? And what does x > y mean in this example? If we speak the condition inside the print function in English we would say "Is True greater than False?" It turns out that because Boolean values are numeric, x is greater than y in this case because True is 1 and False is 0, in Python.
Operator | Comparison |
---|---|
== | Equal |
!= | Not Equal |
> | Greater Than |
< | Less Than |
>= | Greater Than or Equal To |
<= | Less Than or Equal To |
True
False