Boolean Logic
Some Computer Science History…
For Context - What's this Boolean Stuff About?
George Boole
- English Mathematician, Philosopher and Logician in the 1800's
- Proposed that logical propositions can be expressed by algebraic equations (mathematical logic - AND, OR, NOT)
- This idea, now called Boolean logic, is the basis of computer science!
In Python…
As we talked about in the review, Python has a bool type to represent Boolean values
- True or False
- just like other values, can be assigned to variables
- comparison operators (we learned the equality operator) return Boolean values
- Boolean values can be combined into expressions using logical 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:
- == - equals (can be called logical equivalence or equality operator)
- != - not equal
- > - greater than
- < - less than
- >= - greater than / equal
- <= - less than / equal
Comparison Operators Continued
- again - these operators always return a bool
- these operators do what you would expect
- == - returns True if both sides are equal →
- != - returns True if both sides are not equal →
- >, <, >=, <= - returns True if value on the left is greater than, less than, greater than / equal, or less than equal to the value on the right →
- never put equals first on >=, <=
Comparison Operators Have Feelings Too
What do you think will happen if we compare 8 >= "four"? →
Comparison Operators and Different Types
- objects of different types, except different numeric types, are never equal
- equals (==) will always return False for different types →
- not equals (!=) will always return True for different types →
- the <, <=, > and >= operators…
- will raise a TypeError if the objects are of different types that cannot be compared →
- will happily compare numeric types (for example comparing floats and ints works as you'd expect)! →
What are Logical Operators?
Logical Operators are operators that combine Boolean values.
- these operators always return another Boolean value.
- furthermore, these operators can be used to create more complex Boolean expressions.
Three Logical Operators:
- and -
- takes two operands, one on each side
- to return True, both sides of the operator must be True →
- or -
- takes two operands, one on each side
- to return True,at least one side of the operator must be True →
- 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:
That Can Get Complicated!
Yes. We'll use truth tables to show what each operator will return.
- a truth table is a concise table of Boolean values that describes the semantics of an operator
- it will go through each possible combination of operands and specify the resulting Boolean value
- each row will represent a combination of operands
- each column will represent a value of one operand
- …with the exception of the last column, which represents the resulting value
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? →
Truth Table - OR
Let's fill out a truth table for or! →
Truth Table - NOT
Let's fill out a truth table for not! →
Let's Evaluate Some Simple Boolean Expressions
- True and False →
- True and not False →
- True or False →
- True or not False →
- False or False →
- not False and not False →
Chaining Operators
Boolean, comparison and other operators can be combined to create complex Boolean expressions! For example:
Order of Operations / Operator Precedence
The previous summary, but even more summar-ied
- parentheses are evaluated first (obv)
- numeric / string operations (in turn, are also ordered)
- comparison operations (==, !=, >, <, >=, >=)
- not
- and
- or
A Quick Note About Boolean Operators and Style
- it's usually a good idea to use parentheses, at the very least, for readability
- …unless you're chaining the same operator (for example True and True and True)
An Exercise
Evaluate the following boolean expression. →
Another One
Evaluate the following boolean expression. →
One More Please?
Evaluate the following boolean expression. →
Short Circuit Evaluation
- if left hand side of boolean decides the outcome, no need to deal with the remainder of expression
- saves some processin' time!
- for example:
(false and (true and true and true or false))
- can stop at false!
- can be applied internally to a larger more complex boolean expression
- generally, just language implementation details, right?
- but it's a concept you should be aware of
- (perhaps if there's a function on the right side that doesn't get called!)
Short Circuit Evaluation Continued
Can you think of some comparison or logical operators where short circuit evaluation wouldn't make sense?
- obv not - nothing to short circuit!
- == equivalence operations - both sides must be evaluated to test equality
Review
- comparison operators
- ==, !=, >, <, >=, <=
- comparing different types
- logical operators
- and, or, not
- truth tables
- operator precedence