Boolean Logic

Some Computer Science History…

For Context - What's this Boolean Stuff About?

George Boole

George Boole

In Python…

As we talked about in the review, Python has a bool type to represent Boolean values

Comparison Operators

Comparison Operators

Can you guess what some of the comparison operators are? Like I said, you already know one!

There are six comparison operators:

Comparison Operators Continued

Comparison Operators Have Feelings Too

What do you think will happen if we compare 8 >= "four"?

>>> 8 >= "four"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() >= str()

Comparison Operators and Different Types

Logical Operators

What are Logical Operators?

Logical Operators are operators that combine Boolean values.

Three Logical Operators:

  1. and -
    • takes two operands, one on each side
    • to return True, both sides of the operator must be True →
  2. or -
    • takes two operands, one on each side
    • to return True,at least one side of the operator must be True →
  3. not -
    • only takes one operand to the right
    • to return True, the original value on the right must evaluate to False →
    • two nots cancel eachother out (fun!) →

Logical Operators in Action

Note that these are all expressions that result in a value that's of type bool:

>>> True and False
False
>>> True and True
True
>>> True or False
True
>>> not True
False
>>> not not True
True

That Can Get Complicated!

Yes. We'll use truth tables to show what each operator will return.

Truth Table - AND

and takes two operands. Each operand can be True or False (or will evaluate to True or False!).

Can you guess how many possible combinations there are for these two operands? What will the Truth Table look like?

"""
(using p and q to represent the operands
...and t and f for true and false)
 p | q | p and q
----------------
 f | f | f
 f | t | f
 t | f | f
 t | t | t
"""

Truth Table - OR

Let's fill out a truth table for or! →

"""
(using p and q to represent the operands
...and t and f for true and false)
 p | q | p and q
----------------
 f | f | f
 f | t | t
 t | f | t
 t | t | t
"""

Truth Table - NOT

Let's fill out a truth table for not! →

"""
(using p and q to represent the operands
...and t and f for true and false)
 p | not p
-----------
 t | f 
 f | t 
"""

Let's Evaluate Some Simple Boolean Expressions

Chaining Operators

Boolean, comparison and other operators can be combined to create complex Boolean expressions! For example:

100 > 1 and -10 ** 2 <= -100 or "foo" == "foo" + "bar"

True

Order of Operations / Operator Precedence

The previous summary, but even more summar-ied

A Quick Note About Boolean Operators and Style

True and True or False and True and 10 * 10 + 10 == 110 and not False

An Exercise

Evaluate the following boolean expression.

not (8 != 8) or not True  
not False or not True  
True or False
True

Another One

Evaluate the following boolean expression.

"hello" == "world" or 5 + 5 > 8 and 4 * 3 < 16 and 28 != 0
"hello" == "world" or 10 > 8 and 12 < 16 and 28 != 0
False or True and True and True
True

One More Please?

Evaluate the following boolean expression.

((True or False) and not True) and not (False and False)
(True and not True) and not (False and False)
False and True
False

Short Circuit Evaluation

Short Circuit Evaluation Continued

Can you think of some comparison or logical operators where short circuit evaluation wouldn't make sense?

Review

If We Feel Like Continuing