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: True or False.
  • Equality comparison (==) must not be confused with variable assignment (=).

The if Statement

  • An if statement 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 0 evaluates to False, whereas any non-zero numeric value evaluates to True.
  • Turtle graphics state methods like turtle.isdown() and turtle.isvisible() return Boolean values usable in if conditions.

The if-else Statement and Conditional Expressions

  • An if/else statement provides a dual-alternative structure executing one block if the condition is True and an alternate block if False.
  • The if clause and else clause must be vertically aligned at the same indentation level.
  • Conditional expression (ternary operator) syntax: x if condition else y. Evaluates condition; returns x if True, otherwise returns y.

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 if statements place decision structures inside the body of an existing if or else block.
  • The if/elif/else statement provides a multi-way decision chain that tests conditions sequentially until one evaluates to True.
  • A trailing else clause 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 : Returns True if both sub-expressions are True.
    • or : Returns True if either sub-expression is True.
    • 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 is True, the right operand is skipped.
    • For and: If the left operand is False, the right operand is skipped.

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 bool data type and store either True or False.
  • 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: average=grade1+grade2+grade33\text{average} = \frac{\text{grade}_1 + \text{grade}_2 + \text{grade}_3}{3}.
    • If average>60\text{average} > 60, display "Pass"; otherwise display "Fail".
  • Character Inequality Check:
    • Expression evaluating to True if str c is not equal to a single space: c != ' '.
  • Conditional Bonus Calculation:
    • Expression assigning 1000010000 to bonus if goods_sold > 500000, else 00: bonus = 10000 if goods_sold > 500000 else 0.
  • Finding Minimum Value:
    • Code logic to assign the smallest of integer variables x, y, and z to variable min.

Here are practice quiz questions based on your material:

Practice Quiz
  1. Relational Expressions: Given x=10x = 10 and y=7y = 7, evaluate the Boolean truth value of each expression:

    • x==10x == 10
    • x<y+1x < y + 1
    • x!=yx != y
  2. Truthiness in Python: In Python conditional statements, what Boolean value does the integer 00 evaluate to, and what do non-zero numeric values evaluate to?

  3. Operator Precedence: Rank the following operator categories from highest precedence to lowest precedence:

    • Logical operators (not, and, or)
    • Arithmetic operators (+, -, *, /)
    • Relational operators (>, <, ==, !=)
  4. Short-Circuit Evaluation:

    • Under what condition does the or operator stop evaluating the second operand?
    • Under what condition does the and operator stop evaluating the second operand?
  5. Conditional Expressions: Write a single-line conditional expression that assigns 1000010000 to bonus if goods_sold > 500000, and otherwise assigns 00.

  6. String Comparison: How does Python compare strings when using relational operators such as < or >?