Python Conditionals, Boolean Logic, and Control Flow Study Notes

Course Grading & Optional Assignment Logistics

  • Bonus Points Structure for Labs:

    • Completing optional labs (Lab 3 optional, Lab 4 optional, Lab 5 optional, Lab 6 optional, and Lab 7 optional) awards a total of 5050 bonus points added directly to the overall lab grade category.

    • Total base points for the lab category range between 350350 and 400400 points. Adding 5050 bonus points is highly significant and allows a student's final course grade to exceed 100%100\%.

    • Challenge problems grant 22 bonus points, which are significantly less impactful than the 5050 optional lab bonus points.

  • Exam and Quiz Replacement Policies:

    • Completing the Optional Exam 2 (or equivalent designated optional assessment) replaces a student's lowest quiz grade with a score of 100100.

  • Submission Deadlines:

    • Optional Lab Assignments: Must be completed and submitted strictly before the exam.

    • Challenge Problems: Must be completed and submitted before their designated due date.

Fundamentals of Code Branching and Conditionals

  • Execution Paradigms:

    • Sequential / Linear Execution: Standard procedural code execution where an Integrated Development Environment (IDE) evaluates instructions line-by-line sequentially (Step 1Step 2Step 3\text{Step 1} \nrightarrow \text{Step 2} \nrightarrow \text{Step 3}).

    • Branching Execution: Utilizes conditional constructs to dynamically direct program execution down distinct code paths (Path A\text{Path A}, Path B\text{Path B}, Path C\text{Path C}) depending on evaluated outcomes.

  • Boolean Foundation:

    • Code branching relies on Boolean values: True or False.

    • Conditions evaluate expressions to yield a Boolean outcome, determining which branch of code executes.

Relational Operators

  • Definition: Relational operators compare two values/expressions and construct a Boolean result (True or False).

  • Supported Relational Operators:

    • Equality (==): Evaluates whether two values are equal. Returns True if equal, False otherwise.

    • Critical Distinctions: == (two equal signs) is the relational equality operator. = (one equal sign) is the assignment operator used to assign values to variables. Using = in place of == inside conditional statements produces syntax or logical errors.

    • Inequality (!=): Represented by an exclamation mark followed by an equal sign. Evaluates whether two values are not equal. Returns True if aba \neq b, and False if a=ba = b

    • Less Than (<): Evaluates if the left-hand side is strictly smaller than the right-hand side.

    • Greater Than (>): Evaluates if the left-hand side is strictly larger than the right-hand side.

    • Less Than or Equal To (<=): Evaluates if the left-hand side is smaller than or equal to the right-hand side. The syntax must strictly follow the character order of < followed by =.

    • Greater Than or Equal To (>=): Evaluates if the left-hand side is larger than or equal to the right-hand side. The syntax must strictly follow the character order of > followed by =.

  • Evaluation Examples:

    • Checking zero equality: variable1 == 0 evaluates to True if variable1 holds 00, and False if non-zero.

    • Checking non-zero state: variable1 != 0 evaluates to True for any positive or negative value other than 00

    • Storing Boolean results: Relational outputs can be directly assigned to variables: python john_older_than_joe = john_age > joe_age &nbsp;&nbsp;&nbsp;&nbsp;     If john_age is strictly greater than joe_age, john_older_than_joe is assigned the Boolean True; otherwise, it is assigned False.

Boolean Operators and Operations

  • Definition: Boolean operators operate directly between one or two existing Boolean expressions or variables to produce a new Boolean value.

  • Primary Boolean Operators:

    • and (Conjunction): Evaluates to True if and only if both operating Boolean operands are True. If either operand is False, the entire and expression evaluates to False.

    • or (Disjunction): Evaluates to True if at least one operating Boolean operand is True. It evaluates to False only when both operands are False.

    • not (Negation/Inverse): Unary operator that reverses the Boolean state of its operand (not True becomes False; not False becomes True).

  • Logical Equivalences:

    • The expression a and b is logically equivalent to not (not a or not b).

  • Compound Boolean Expression Examples:

    • Range Checking (0 \nle \text{variable1} \nle 100): python (variable1 >= 0) and (variable1 <= 100) &nbsp;&nbsp;&nbsp;&nbsp;     Alternative using not and or: python not (variable1 < 0 or variable1 > 100) &nbsp;&nbsp;&nbsp;&nbsp;

    • Equality and Positive Check: python (variable1 == variable2) and (variable1 > 0) &nbsp;&nbsp;&nbsp;&nbsp;

    • Water State / Liquid Phase Evaluation: python is_liquid = (temperature >= 32) and (temperature <= 212) &nbsp;&nbsp;&nbsp;&nbsp;     Evaluates to True if temperature is between 3232 and 212212 inclusive (in degrees Fahrenheit). If temperature is outside this range, one or both sub-conditions evaluate to False, causing is_liquid to become False.

Evaluation and Order of Operations

  • Precedence Hierarchy:

    1. Parentheses: Expressions within () are evaluated first.

    2. Arithmetic / Mathematical Operators: Evaluated following standard mathematical rules (exponentiation, multiplication, division, addition, subtraction).

    3. Relational Operators: (==, !=, <, >, <=, >=) evaluated next.

    4. Boolean Operators: Evaluated in the strict sequential order:

    • First: not

    • Second: and

    • Third: or

  • Detailed Precedence Tracing Problem:

    • Initial Variables: a=10a = 10, b=10b = 10, c=20c = 20

    • Target Logic Evaluation:

    • Relational Evaluations First:

      • a>b10>10Falsea > b \nrightarrow 10 > 10 \nrightarrow \text{False}

      • b \nle c \nrightarrow 10 \nle 20 \nrightarrow \text{True}

      • c \nle 20 \nrightarrow 20 \nle 20 \nrightarrow \text{True}

      • b==1010==10Trueb == 10 \nrightarrow 10 == 10 \nrightarrow \text{True}

      • c102010Truec \neq 10 \nrightarrow 20 \neq 10 \nrightarrow \text{True}

    • Boolean Operator Precedence Application (not -> and -> or):

      • Evaluate and operations: False and True yields False; True and True yields True.

      • Apply not inversion: not True yields False.

      • Evaluate or operations: False or False yields False.

Conditional Statements: If, If-Else, and If-Elif-Else Blocks

  • Syntax and Structural Rules of if Statements:

    • Structure: python if condition: # Indented block of code ("things to do") &nbsp;&nbsp;&nbsp;&nbsp;

    • Colon Requirement: A colon : must immediately follow the conditional expression.

    • Indentation Requirement: Python strictly mandates consistent indentation (typically a 4-space tab) for the code block inside the condition. Failing to indent results in a SyntaxError.

    • Execution Logic: The indented block executes only if the condition evaluates to True. If the condition evaluates to False, the entire indented block is skipped.

  • Code Tracing Examples:

    • Literal Boolean Example: python if True: print("Howdy") print("World") &nbsp;&nbsp;&nbsp;&nbsp;     Outputs both `