Home » Chapter 3 : Decisions
Conditions & Booleans
In programming, the concepts of conditions and booleans are fundamental building blocks that enable us to create programs that can make decisions and respond differently to varying circumstances. At its core, a boolean is a type of data that can only have one of two values: True or False. Think of it as a simple yes-or-no switch. This simplicity is deceptive though, as booleans form the foundation of conditional statements, which are used to execute different pieces of code based on certain conditions. A conditional statement in Python, often expressed through if, elif, and else clauses, allows the program to evaluate a boolean expression and then take action accordingly. This ability to make decisions is what brings a program to life, allowing it to interact with data in complex and meaningful ways, much like how a storyteller crafts a narrative based on the decisions of their characters. For students and practitioners of humanities, understanding these concepts opens up many possibilities for data analysis, textual processing, and interactive storytelling, where each branching path or outcome can be meticulously crafted and explored through the logical structuring of conditions and booleans in Python.
Figure 1: General form of a condition in programming.
Figure 2: Example of a condition in programming.
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.
Operator | Description |
---|---|
and | The and operator compares the condition to its left with the condition to its right, if both conditions are True then the compound condition is True. |
or | The or operator compares the condition to its left with the condition to its right, if either of the conditions is True then the compound condition is True. |
not | The not operator reverses the result of the compound condition, so if the compound returns True the not operator will reverse it to False, and visa versa. |
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