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 bonus points added directly to the overall lab grade category.
Total base points for the lab category range between and points. Adding bonus points is highly significant and allows a student's final course grade to exceed .
Challenge problems grant bonus points, which are significantly less impactful than the 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 .
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 ().
Branching Execution: Utilizes conditional constructs to dynamically direct program execution down distinct code paths (, , ) depending on evaluated outcomes.
Boolean Foundation:
Code branching relies on Boolean values:
TrueorFalse.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 (
TrueorFalse).Supported Relational Operators:
Equality (
==): Evaluates whether two values are equal. ReturnsTrueif equal,Falseotherwise.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. ReturnsTrueif , andFalseifLess 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 == 0evaluates toTrueifvariable1holds , andFalseif non-zero.Checking non-zero state:
variable1 != 0evaluates toTruefor any positive or negative value other thanStoring Boolean results: Relational outputs can be directly assigned to variables:
python john_older_than_joe = john_age > joe_age Ifjohn_ageis strictly greater thanjoe_age,john_older_than_joeis assigned the BooleanTrue; otherwise, it is assignedFalse.
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 toTrueif and only if both operating Boolean operands areTrue. If either operand isFalse, the entireandexpression evaluates toFalse.or(Disjunction): Evaluates toTrueif at least one operating Boolean operand isTrue. It evaluates toFalseonly when both operands areFalse.not(Negation/Inverse): Unary operator that reverses the Boolean state of its operand (not TruebecomesFalse;not FalsebecomesTrue).
Logical Equivalences:
The expression
a and bis logically equivalent tonot (not a or not b).
Compound Boolean Expression Examples:
Range Checking (0 \nle \text{variable1} \nle 100):
python (variable1 >= 0) and (variable1 <= 100) Alternative usingnotandor:python not (variable1 < 0 or variable1 > 100) Equality and Positive Check:
python (variable1 == variable2) and (variable1 > 0) Water State / Liquid Phase Evaluation:
python is_liquid = (temperature >= 32) and (temperature <= 212) Evaluates toTrueiftemperatureis between and inclusive (in degrees Fahrenheit). Iftemperatureis outside this range, one or both sub-conditions evaluate toFalse, causingis_liquidto becomeFalse.
Evaluation and Order of Operations
Precedence Hierarchy:
Parentheses: Expressions within
()are evaluated first.Arithmetic / Mathematical Operators: Evaluated following standard mathematical rules (exponentiation, multiplication, division, addition, subtraction).
Relational Operators: (
==,!=,<,>,<=,>=) evaluated next.Boolean Operators: Evaluated in the strict sequential order:
First:
notSecond:
andThird:
or
Detailed Precedence Tracing Problem:
Initial Variables: , ,
Target Logic Evaluation:
Relational Evaluations First:
b \nle c \nrightarrow 10 \nle 20 \nrightarrow \text{True}
c \nle 20 \nrightarrow 20 \nle 20 \nrightarrow \text{True}
Boolean Operator Precedence Application (
not->and->or):Evaluate
andoperations:False and TrueyieldsFalse;True and TrueyieldsTrue.Apply
notinversion:not TrueyieldsFalse.Evaluate
oroperations:False or FalseyieldsFalse.
Conditional Statements: If, If-Else, and If-Elif-Else Blocks
Syntax and Structural Rules of
ifStatements:Structure:
python if condition: # Indented block of code ("things to do") 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 toFalse, the entire indented block is skipped.
Code Tracing Examples:
Literal Boolean Example:
python if True: print("Howdy") print("World") Outputs both `