6. Propositional Logic
Sometimes things are
TrueorFalseThis course focuses on introductory computer science —
TrueThis topic is titled “Not Logic” —
False
You already use this type of logic in everyday 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 differently
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
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 and Values
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
Like other types of values, they can be assigned to variables
some_boolean = Truesome_other_boolean = False
6.2.1. Boolean Operators
Just like the arithmetic operators we use on integers and floats, Booleans have operators too
The ones we just used
andornot
Consider the arithmetic 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
1is_raining = True
2is_cold = False
3
4print(is_raining and is_cold) # False
5print(is_raining or is_cold) # True
6print(not is_raining) # False
6.2.2. Comparison Operators
Writing
True and Falseisn’t very useful on its ownBased on 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 takes 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 isFalse, 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
Note
The above examples are intentionally complex. In general, we avoid writing long and complex boolean expressions.
6.4. For Next Topic
If you have not yet, read Chapter 5 of the text