Python Program Control Structures: Decisions, Loops, and Boolean Logic

Fundamentals of Control Structures and Simple Decisions

  • Sequential Program Execution Limitations:

    • In standard programming, instructions execute sequentially, one after another.

    • Sequential flow alone is insufficient for complex problem-solving where programs must adapt actions based on changing circumstances or conditions.

  • Control Structures and Decision Structures:

    • Control structures are programming constructs that alter the sequential flow of program execution.

    • Decision structures are statements that allow a program to execute different sequences of instructions for different cases, enabling selective branching based on condition evaluation.

  • Simple Decisions (One-Way Decisions):

    • A simple decision executes an indented block of code if, and only if, a specified condition is true.

    • Syntax: python if <condition>: <body> &nbsp;&nbsp;&nbsp;&nbsp;

  • Execution Semantics:

    • The condition in the if heading is evaluated first.

    • If the condition evaluates to true, the statements in <body> execute sequentially, and control then passes to the subsequent statement following the if block.

    • If the condition evaluates to false, the statements in <body> are skipped completely, and control passes directly to the next statement outside the structure.

    • The body can comprise one or more statements indented beneath the if line.

Flowchart of a generic one-way decision structure
  • Temperature Warning Program Example:

    • Initial sequential conversion program (convert.py) written by Susan Computewell: python # convert.py # A program to convert Celsius temps to Fahrenheit # by: Susan Computewell def main(): celsius = eval(input("What is the Celsius temperature? ")) fahrenheit = 9/5 * celsius + 32 print("The temperature is", fahrenheit, "degrees Fahrenheit.") main() &nbsp;&nbsp;&nbsp;&nbsp;

  • Specification for Warnings:

    • Temperatures above 9090 degrees Fahrenheit trigger a heat warning.

    • Temperatures below 3030 degrees Fahrenheit trigger a cold weather warning.

  • Pseudocode Algorithm:

    • 1.0 Input the temperature in degrees Celsius

    • 2.0 Calculate fahrenheit as 9/5 celsius + 32

    • 3.0 Output fahrenheit

    • 4.0 If fahrenheit > 90

    • 4.1 print a heat warning

    • 5.0 If fahrenheit < 30

    • 5.1 print a cold warning

Flowchart of temperature warning decision structure
  • Implementation (convert2.py): ```python

    convert2.py

    A program to convert Celsius temps to Fahrenheit.

    This version issues heat and cold warnings.

    def main(): celsius = eval(input("What is the Celsius temperature? ")) fahrenheit = 9 / 5 * celsius + 32 print("The temperature is", fahrenheit, "degrees fahrenheit.") if fahrenheit > 90: print("It's really hot out there, be careful!") if fahrenheit < 30: print("Brrrrr. Be sure to dress warmly") main()     ```

  • Single vs. Multi-Statement Condition Blocks:

    • A simple decision can guard a single assignment statement, such as setting bonus = 500.0 when sales > 50000:

Flowchart of single-statement decision logic for sales bonus
  • A simple decision can also guard a compound sequence of multiple statements, such as setting bonus = 500.0, assigning commission_rate = 0.12, and displaying print('You met your sales quota!'):

Flowchart of multi-statement decision logic for sales bonus and commission

Forming Conditions and Relational Operators

  • Structure of Conditions:

    • Simple comparison syntax follows the general form: <expr> <relop> <expr> where <relop> is a relational operator.

  • Relational Operators in Python vs. Mathematics:

    • < : Mathematics << : Less than

    • <= : Mathematics ≤\le : Less than or equal to

    • == : Mathematics == : Equal to

    • >= : Mathematics ≥\ge : Greater than or equal to

    • > : Mathematics >> : Greater than

    • != : Mathematics ≠\ne : Not equal to

Table of Python relational operators, mathematical notations, and meanings
  • Equality vs. Assignment:

    • Python distinguishes the assignment operator = from the comparison operator ==.

    • Using = inside an if condition heading is a syntax error.

  • Numeric and String Comparisons:

    • Conditions compare numeric values as well as character strings.

    • String comparisons follow lexicographic ordering determined by underlying Unicode integer values.

    • All uppercase characters precede lowercase characters in Unicode; consequently, `