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>
Execution Semantics:
The condition in the
ifheading is evaluated first.If the condition evaluates to true, the statements in
<body>execute sequentially, and control then passes to the subsequent statement following theifblock.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
ifline.

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()
Specification for Warnings:
Temperatures above degrees Fahrenheit trigger a heat warning.
Temperatures below degrees Fahrenheit trigger a cold weather warning.
Pseudocode Algorithm:
1.0 Input the temperature in degrees Celsius2.0 Calculate fahrenheit as 9/5 celsius + 323.0 Output fahrenheit4.0 If fahrenheit > 904.1 print a heat warning5.0 If fahrenheit < 305.1 print a cold warning

Implementation (
convert2.py): ```pythonconvert2.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.0whensales > 50000:

A simple decision can also guard a compound sequence of multiple statements, such as setting
bonus = 500.0, assigningcommission_rate = 0.12, and displayingprint('You met your sales quota!'):

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: Less than or equal to==: Mathematics: Equal to>=: Mathematics: Greater than or equal to>: Mathematics: Greater than!=: Mathematics: Not equal to

Equality vs. Assignment:
Python distinguishes the assignment operator
=from the comparison operator==.Using
=inside anifcondition 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, `