As programmers, we often need to be able to control when specific lines of code are executed. A common way to do this is by using logic statements, also called boolean expressions. These expressions are a series of conditions that evaluate to True
or False
depending on the state of the conditions and the logic involved.
Let’s take a simple first example: the statement it is currently raining
is either True
or False
. Evaluating this will determine if we need to grab an umbrella or not. In order to implement this sort of statement into our programs, we use a specific type of variable, called a boolean or bool for short.
A boolean is a type of variable that can hold the value of either True
or False
, and nothing else. Compare this to an integer variable that can hold any whole number, or a string variable that can hold any text.
Take a look at the example below where we use a boolean to implement the rainy situation from above.
is_raining
and see what happens (remember, a boolean can only be one of two values)!In many cases, our condition is as simple as the one above (if one condition is true, then perform one action), however, there are also many situations where there is more than one boolean variable involved and we need to evaluate them together.
Extending our current example, we know if it is raining we need to grab an umbrella… but
True
, so we’ll need to grab a raincoat as well. True
AND one condition is NOT True
. In this case, we will grab a sweatshirt instead of a raincoat.You can imagine that this can get quite complicated! The key here is understanding our three Logical Operators that create our boolean expressions:
and – evaluates to
True
only if both variables areTrue
, and evaluates toFalse
otherwise
or – evaluates toTrue
if at least one of the variables isTrue
, and evaluates toFalse
if both variables areFalse
not – evaluates toTrue
if the variable isFalse
, and evaluates toFalse
if the variable isTrue
. Essentially it takes the opposite value
Python makes it super easy to use these operators in our code – we just use the lowercase words! Using them with the situations above, we’d get:
Raining
and
cold? –> Raincoat
Coldand not
raining? –> Sweatshirt
Rainingor
sunny? –> Umbrella
Check out the program below to see this in action. Try changing the values of the booleans at the top and see how it affects the output!
As you’re beginning to see, setting up and evaluating boolean expressions can range from very simple to rather complex, depending on the needs of your program. Let’s do some extra practice so you can perfect your skills!
The example below is set up with a series of initialized booleans. Before running the program, read through each boolean expression and try to determine if it will evaluate to True
or False
. Type your answers in the comment lines immediately below, then run it and see how you did! Hint: similar to order of operations in math, you’ll want to evaluate the expressions inside the parentheses first.
How’d you do?? For the next practice, take the below statements and write them as boolean expressions (this is NOT a runnable program, just a free response). You can create whatever boolean variables you need. I’ve done the first one for you.