2-2 Boolean Logic

Logical Operators

So far our conditions have involved a single Boolean expression.  It’s possible to create compound Boolean expressions that combine more than one simple Boolean expression in different ways.  Python provides three logical operators to support this.

    • and — Connects two Boolean expressions into one compound expression.  Both sub-expressions must be True for the overall compound expression to be True.
    • or — Connects two Boolean expressions into one compound expression.  One or both sub-expressions must be True for the compound expression to be True.  This is a little counter-intuitive because in English we think of “or” as meaning one or the other, but not both (i.e., an exclusive-or).  In programming, “or” means one, or the other, or both.  It might help to think of this like an “and/or”.
    • not — Is a unary operator, meaning that it works with only one operand, which must be a Boolean expression.  The not operator reverses the truth of its operand.  For example, if its Boolean expression is True, the not operator returns False, and vice-versa.

The truth table below summarizes the differences between the and and or operators:

Now let’s apply this to a Python example:

Notice how the and and or operators are used to connect two Boolean expressions.  The parentheses ( ) are optional but help improve readability when you have more than one sub-expression.  I recommend that you include them.

The first compound condition uses the and operator to check if the temperature is both greater than or equal to 20 and less than 28.  If one or the other, or both, of these two sub-expressions is False, then the overall result will also be False.  The result will only be True if both sub-expressions are true.

The second compound condition uses the or operator to check if the temperature is less than 20 or greater than or equal to 28.  If one or the other, or both, of these two sub-expressions is True, then the overall result will be True.  Although, in this example, it’s mathematically impossible for both sub-expressions to be True.  The result will only be False if both sub-expressions are False.

For efficiency, Python uses what is known as short-circuit evaluation.  This sounds like a bad thing, but it’s actually a very clever idea.  With an and expression, if the expression on the left side of the and operator is False, then Python returns a False result immediately.  The Python interpreter does not evaluate the expression on the right side since the overall result can never be True.

Similarly, with or, if the expression on the left side of the or operator is True, then Python returns a True result immediately.  The expression on the right side will not be evaluated.  Since it is only necessary for one of the expressions to be True, it would waste CPU time to check the right-side expression if the left-side has already been evaluated to True.

Based on the example above, I hope you can see that when you are checking to see if a number is within a certain range the and operator should be used, and when checking to see if a number is outside a certain range the or operator should be used.  Be careful not to get your and and or logic confused when testing for a range of numbers like this.  For example,

This compound condition will never be True.  The value that temperature refers to will never be less than 20 and at the same time greater than 40.  It’s mathematically impossible!

Here is the truth table for the not operator:

And a Python example:

This expression is basically asking “Is the temperature not greater than 100?”.  If the temperature is greater than 100, the result of the expression in brackets will be True.  The not operator will cause False to be returned instead.  If the temperature is less than or equal to 100, the result of the expression will be False, and the not operator will return True instead.

In the example above, the parentheses ( ) are necessary to ensure that the not operator is applied to the value of the expression temperature > 100, not just to the temperature variable.

Boolean Variables

So far we have learned about the int, float, and str data types.  Python also supports a bool data type for Boolean variables.  A Boolean variable may only reference one of two values: True or FalseBoolean variables are commonly referred to as flags.  A flag is a Boolean variable that signals when some condition exists in the program.  Note that the values True and False must start with an uppercase letter.

Here’s an example:

Notice that we didn’t need to compare if high_score == True, but we (optionally) could have:

Interpreting Any Value as True or False

Look at the condition in the if statement below:

Notice that money is not compared to any other value, money is the condition.  When it comes to evaluating numbers, 0 is False and everything else is True.  So, the condition in the if statement is equivalent to:

The original version is simpler, more elegant, and more intuitive.  It reads more naturally and could be translated to “if there is money”.  The rule for what makes a value True or False is simple: any empty or zero value is False, everything else is True.  Let’s use the built-in bool() function to convert a few different values and see what we get:

You Try!

    1. Start a new page in your Learning Journal titled “2-2 Boolean Logic“.  Carefully read the notes above and in your own words summarize the key ideas from each section.  Think of it as writing a “cheat sheet” for this lesson.   Making these notes will help solidify your understanding of the material and serve as a future reference to help you review key concepts.
    2. Assume the variables a = 2, b = 4, and c = 6. State the Boolean result of each of the following conditions:
    3. Write an if..else statement that determines whether a variable called points is outside the range of 9 to 51 (exclusive).  If the variable is outside this range it should display “Invalid points.”  Otherwise, it should display “Valid points.”
    4. Write an if..else statement that determines whether a variable called speed is inside the range of 0 to 200 (inclusive).  If the variable is inside this range it should display “Valid Speed.”  Otherwise, it should display “Invalid Speed.”