Decision Structures and Boolean Logic Notes
Relational Operators and Expressions
- Relational operators compare two values (numbers or strings) to determine their relative order:
>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to==: Equal to!=: Not equal to
- Relational expressions evaluate to Boolean values:
TrueorFalse. - Equality comparison (
==) must not be confused with variable assignment (=).
The if Statement
- An
ifstatement is a control structure that enables conditional execution of statements (one-way decision). - Syntax requirements: A colon
:follows the Boolean condition, and the controlled body of statements must be indented. - Python truthiness rules: Numeric value
0evaluates toFalse, whereas any non-zero numeric value evaluates toTrue. - Turtle graphics state methods like
turtle.isdown()andturtle.isvisible()return Boolean values usable inifconditions.
The if-else Statement and Conditional Expressions
- An
if/elsestatement provides a dual-alternative structure executing one block if the condition isTrueand an alternate block ifFalse. - The
ifclause andelseclause must be vertically aligned at the same indentation level. - Conditional expression (ternary operator) syntax:
x if condition else y. Evaluatescondition; returnsxifTrue, otherwise returnsy.
String Comparison
- Strings are compared character-by-character based on their ASCII character values.
- String comparisons are case-sensitive.
- If a shorter string is a proper prefix/substring of a longer string, the longer string is evaluated as greater.
Nested if and if-elif-else Decision Structures
- Nested
ifstatements place decision structures inside the body of an existingiforelseblock. - The
if/elif/elsestatement provides a multi-way decision chain that tests conditions sequentially until one evaluates toTrue. - A trailing
elseclause serves as a default fallback to handle remaining cases or catch invalid user inputs.
Logical Operators and Precedence
- Logical operators combine relational expressions into compound Boolean expressions:
and: ReturnsTrueif both sub-expressions areTrue.or: ReturnsTrueif either sub-expression isTrue.not: Reverses the truth value of an expression.
- Logical operator precedence order:
not(highest) >and>or(lowest). - Overall operator precedence: Arithmetic operators > Relational operators > Logical operators.
- Short-circuit evaluation stops evaluating compound expressions as soon as the outcome is guaranteed:
- For
or: If the left operand isTrue, the right operand is skipped. - For
and: If the left operand isFalse, the right operand is skipped.
- For
Numeric Ranges, Input Validation, and Flags
- Ranges can be checked using logical operators (e.g.,
grade >= 0 and grade <= 100) or chained relational operators (e.g.,0 <= grade <= 100). - Input validation inspects user data prior to processing to prevent invalid inputs or division-by-zero errors.
- Boolean variables belong to the
booldata type and store eitherTrueorFalse. - A flag is a Boolean variable used to signal the presence or absence of a specific condition in a program.
Questions & Discussion
- Three-Grade Average Task:
- Prompt user for 3 test grades.
- Compute sum and average: .
- If , display
"Pass"; otherwise display"Fail".
- Character Inequality Check:
- Expression evaluating to
Trueif strcis not equal to a single space:c != ' '.
- Expression evaluating to
- Conditional Bonus Calculation:
- Expression assigning to
bonusifgoods_sold > 500000, else :bonus = 10000 if goods_sold > 500000 else 0.
- Expression assigning to
- Finding Minimum Value:
- Code logic to assign the smallest of integer variables
x,y, andzto variablemin.
- Code logic to assign the smallest of integer variables
Here are practice quiz questions based on your material:
Practice Quiz
Relational Expressions: Given and , evaluate the Boolean truth value of each expression:
Truthiness in Python: In Python conditional statements, what Boolean value does the integer evaluate to, and what do non-zero numeric values evaluate to?
Operator Precedence: Rank the following operator categories from highest precedence to lowest precedence:
- Logical operators (
not,and,or) - Arithmetic operators (
+,-,*,/) - Relational operators (
>,<,==,!=)
- Logical operators (
Short-Circuit Evaluation:
- Under what condition does the
oroperator stop evaluating the second operand? - Under what condition does the
andoperator stop evaluating the second operand?
- Under what condition does the
Conditional Expressions: Write a single-line conditional expression that assigns to
bonusifgoods_sold > 500000, and otherwise assigns .String Comparison: How does Python compare strings when using relational operators such as
<or>?