6. Propositional Logic
Sometimes things are
TrueorFalseThis course focuses on introductory computer science —
TrueThis topic is titled “Not Logic” —
False
This is rather intuitive since you use this type of simple logic in every day life to make decisions
Additionally, you are already familiar with some operators we can use on these
True/Falsestatementsandornot
6.1. Truth Table
A |
B |
A |
A |
|
|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The above truth table is a rather formal representation of some everyday ideas
To put it slightly different
Is the sky blue
andis water wet? —TrueIs the sky blue
andis it over 100 degrees Celsius outside? —FalseIs the sky blue
oris it over 100 degrees Celsius outside? —TrueIs it
notover 100 degrees Celsius outside? —TrueIs the sky blue
andis itnotover 100 degrees Celsius outside? —True
You may have observed that
For
and, both statements must beTrueto produceTrue, otherwise it isFalseFor
or, only one statement must beTrueto produceTrue, otherwise it isFalsenotchangesTrue->FalseandFalse->True
Note
For or, both statements being True produces True. There is another operator called exclusive or that
is True only when one of the statements is True. Exclusive or is not going to come up in this course and it
is only noted here since some people find or ambiguous at first.
6.2. Boolean Type
Booleans are a type in Python, like integers, strings, and floats
type(True)gives usbool
Unlike integers, strings, and floats which can take on many different values, booleans can only be one of two values
TrueFalse
6.2.1. Boolean Operators
Just like the arithmatic operators we use on integers and floats, Booleans have operators too
The ones we just used
andornot
Consider the arithmatic operator
+It takes two number operands
It produces a new number as a result
e.g.,
1 + 2is3
For Booleans, consider
andIt takes two Boolean operands
It produces a new Boolean as a result
e.g.,
True and FalseisFalse
6.2.2. Comparison Operators
As you have probably noticed, asking
True and Falseis not overly helpful as it isBased how we use this logic in real life, we need a way to evaluate statements into their Boolean values
For example, is it
notover 100 degrees Celsius outside?We need a way to check if the given temperature is greater than 100 degrees Celsius
For these situations, we make use of comparison operators you already use in your everyday life
Is five greater than three?
5 > 3isTrue
More generally, these comparison operators take values to be compared
Consider the greater than (
>) comparison operatorIt two values as operands
It produces a Boolean as a result
e.g.,
1 > 7isFalse
The comparison operators we make use of are
Equal
==Not equal
!=Greater than
>Greater than or equal
>=Less than
<Less than or equal
<=
Activity
Play around with the above operators on different integers and see if you can find operands that produce
True/False for each.
Play around with the operators on different types. For example, what happens when you compare Booleans, floats, and strings?
Warning
Mind the use of two equal signs (==) for checking equality. Remember, a single equals sign (=) is the
assignment operator.
some_variable = 5assigns the value5to the variablesome_variable
some_variable == 5checks if the value stored insome_variableis equal to the value5
6.3. Composing Operators and Values
We have seen different operators that work on different types
As long as the types check out and we follow the order of operations, we can compose rather complex expressions
Don’t worry about memorizing the order of operations
They follow what you are used to for the most part
When in doubt, make use of parentheses
6.3.1. Evaluating Example Expressions
((17 + 2) < 18) or (17 != 18)
(19 < 18) or (17 != 18)
False or (17 != 18)
False or True
True
(101 == 100) and ((66 - 17) < 54))
False and ((66 - 17) < 54)
False and (49 < 54)
False and True
False
Once we evaluated
(101 == 100)asFalse, we didn’t need to evaluate the remainder of the expressionWith
and, if one of the operands areFalse, the whole expression evaluated toFalse
(14 > 0) or ((6 != 7) and ((4 + 17) < 20))
True or ((6 != 7) and ((4 + 17) < 20))
True or (True and ((4 + 17) < 20))
True or (True and (21 < 20))
True or (True and False)
True or False
True
Notice that once we evaluated
(14 > 0)asTrue, we really didn’t need to finish evaluating the remainder of the expressionThis is because, as long as one of the operands for an
orisTrue, we know the whole expression isTrue
Warning
If you find yourself writing long and complex boolean expressions, chances are you are doing something wrong. Even if we have a long list of conditions you need to check in your program, there are ways to make them more manageable and easier to follow.
6.4. For Next Class
If you have not yet, read Chapter 5 of the text