Subscribe Contact

Home  »  Chapter 3 : Decisions
Conditions & Booleans

Overview

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.

Conditions

Concept: Conditions

A condition is a statement or expression that evaluates to either a True or False state, and is typically used to guide the flow of execution through a program. The result of evaluating a condition is often used to make a decision. For example, if we wrote a simple guessing game, where we prompt the user for a number between a low and high range, we can and if they guess right we let them know they guessed right.



Figure 1 depicts the general form of a condition in programming. Beginning at the top of the diagram, the flow of execution indicates teh set of steps in a program that occur before the condition. When the condition is encountered (often called a conditional statement) and is evaluated a result of either True or False is determined. As the diagram show, if the result is True, then the statements on the left are executed. If the result of evaluating the condition is False, then the statements on the right are executed.

Figure 2 depicts an example of a conditional statement, which asks the question "Is today Saturday?". As the diagram shows, if it is Saturday, then the condition evaluates to True and the subsequent instruction is to do the laundry. If the condition evaluates to False, that is, it is not Saturday, then the instruction is to not do the laundry.

Figure 1: General form of a condition in programming.

Figure 2: Example of a condition in programming.


Booleans

Concept: Booleans

A 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.

Logical Operators

First let's remind ourselves about the Logical Operators as we'll need these in our discussion.

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.

On the previous few pages, we explored decision statements: if, if-else, and if-elif-else. Decision statements are also called boolean expressions because their result result in either a True or False value. In those expressions, we use one or more of the operators listed above to create the question to which we want the True or False answer.

Let's use one of the questions above as an example: Is the user employed?

If you recall from our earlier discussion about Data and Data Types, I used an example of prompting a user for information about them. One of the questions was whether or not they are employed. When our program collects the input from the user, the variable that we use to store their answer to that question should be of type bool. In this way, we have their answer in a form that we can easily evaluate because the variable employed contains our bool value which contains either True or False. So, then we can write code like this:

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.

Concept: Evaluation

Evaluation: I have used the term evaluate several times so far, but what does that mean in the context of programming? Evaluation in programming means that we examine the result of something and, usually, take some action based on the result of the evaluation. So, for example, if I write

x = 10
y = 2
z = x * y
How might I examine the value of z? I could print it, but what if I needed to know whether the value of z is above or below a certain value? I can evaluate that question as a decision (a.k.a. a conditional statement).
if z > 15:
# do something
With this if statement I am evaluating the value of z as a condition, in this example the condition of z > 15 will evaluate to True, so my # do something code will run. Content


Understanding the evaluation of boolean logic leads us to the concept of Truth Tables

Concept: Truth Tables

Sets of rows and columns used to evaluate True/False conditions (often called propositions) of a logical problem. The table is intended to compare all possible propositions (combinations) that make up the resulting values of the problem. We can use truth tables in programming to aid in designing solutions.


Truth Tables


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

Question 1

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


Question 2

Explain the results displayed in the fifth column for this truth table where w and (x or y) and (z or w).



Booleans are Numeric

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.

Let's remind ourselves about the Comparison Operators and then we'll consider them as they are related to truth values.

Comparison Operators

Operator Comparison
== Equal
!= Not Equal
> Greater Than
< Less Than
>= Greater Than or Equal To
<= Less Than or Equal To

So, when we run the code segment above the output is:

True
False

The reason is that True (1) is greater than False (0) so the first print statement prints True because the evaluation of the condition x > y is true. The second print statement evaluates y > x and results in False, because False (0) is less than True (1).

Given everything we've learned on this page thus far when we need to make comparisons of compound conditions we now know we can use truth tables, logical operators, and comparison operators with logical variables and expressions. We will be using these concepts repeatedly as we progress and we will expand on these ideas as well.



 


«  Previous : Decisions : Concepts
Next : Decisions : if Statements  »



© 2023 John Gordon
Cascade Street Publishing, LLC