Decisions and Selection
How to negate a logical expression (DeMorgan's Laws)
- Boolean Values and Boolean Expressions
- need way to express the results of a comparison
- usually some form of True and False - how all computer logic works
- called Boolean values (George Boole) or Boolean constants or literals
- Python uses True, False - they are NOT strings (no quotes)
- relational operators (book calls them comparison operators)
- ==, !=, <=, <, >, >=
- compare all types of data (string to string, int to int,
float to int, float to float)
- produce True or False
- note the difference between = and ==
- Logical operators
- Precedence of Operators
- This table comes from docs.python.org - highest precedence at top, lowest at bottom, some operators removed
|x[index], x[index:index], x(arguments...), x.attribute ||Subscription, slicing, call, attribute reference
|** ||Exponentiation (right to left associative) |
|+x, -x, ~x ||Positive, negative, bitwise NOT|
|*, /, //, % ||Multiplication, division, remainder|
|+, - ||Addition and subtraction|
|in, not in, is, is not, <, <=, >, >=, !=, == ||Comparisons, including membership tests and identity tests|
|not x ||Boolean NOT|
|and ||Boolean AND|
|or ||Boolean OR|
- So in an expression like a + b * 5 > z - 3**y, the exponentiation
is done first, then the multiplication, then the addition and the subtraction,
then the comparison, to finally result in a bool value
- Conditional Execution: Binary Selection
- Omitting the else Clause: Unary Selection
- Nested conditionals
- can nest one if statement inside another
- the indentation tells which else goes with which if
indentation is important!
if a > 5:
if b < 10:
if a > 5:
if b < 10:
- this structure is most useful when you have a set of
conditions which covers ALL possibilities
- see flowchart in textbook
- does not have to end with a plain "else:" but it can
- if have plain else at end, it catches all values which
did not match any of the tests above (kind of "default" case)
- more efficient than having a bunch of separate ifs - as soon
as one is found that is true, the rest are skipped
- do NOT have to repeat tests in later if's which were already
found to be true! do not do this!
if x > 50:
grade = "A"
if x <= 50 and x > 30: #the x <= 50 MUST be true! don't test again!
grade = "B"
- example: quadrant on Cartesian plane
- Chained conditionals
- Notes on 6.7.1 question
- choice I has a syntax error so it will not run
- choice II has some extra parentheses it does not need around
the condition, but that does not break any syntax rules
- pay attention to what choice III is - it is not at all the same
as choice II. It is two if statements, not one.
Choice II is only one statement - all the phrases are tied
together. Choice III has one if with NO else, then another if statement
with an else.
- Boolean Functions
- functions can return Boolean values
- usually name them as questions "isError" or "canFind"
- "return x == 0" would test x (assumed to be the parameter)
and return True if x was 0, False otherwise
- can call any function in the condition of an if, including
- don't have to say "isPrime(num) == True" - this means the same
as the example
- the example in the textbook is dangerous - does not check
for division by zero before it divides by y!
- PLEASE use ONE return statement in functions like this!
if x > 0:
return "not positive"
But this breaks a rule of structured programming, for each structure to have ONE entrance
and ONE EXIT!
- Instead use a variable to hold the result until the end
and return the variable
if x > 0:
result = "positive"
result = "not positive"
- a boolean function should have a statement as part of its
documentation that says "returns True if .... and False when ..."
- sometimes have a complicated expression using several operators
like "a > 5 and b < 7 or c == 5"
- you discover that you actually need the negative of the expression
- you can use the not operator and parentheses to get the reverse
like "not (a > 5 and b < 7 or c == 5)"
- OR you can "carry through" the not through the whole expression
- DeMorgan's Laws not (a and b) is equivalent to (not a or not b)
- and not (a or b) is equivalent to (not a and not b)
- so "not (a > 5 and b < 7 or c == 5)" is the same as
"a <= 5 or b >= 7 and c != 5"
Comparing float values
- also some discussion of this in Chapter 8 (Strings)
- relational operators work with strings too!
- compared by comparing ASCII codes of characters
- if all characters are equal, the 2 strings are equal.
otherwise the string with the character with the smaller ASCII
value is the "lesser" string
- space < digits < uppercase letters < lowercase letters
- " " < "0" < ... < "9" < "A" < ... < "Z" < "a" < ... < "z"
Thinking about if's short
set of slides about putting if's together