Section 1.2: Control Flow
A program that runs top to bottom once can only ever do one fixed thing. Control flow is what lets it decide and repeat: taking one branch instead of another based on a condition, running a block until something changes, and walking every item of a collection in turn. The subtlety is not the syntax, it is truthiness, the rule Python uses to treat non-boolean values as true or false, and the loop that never terminates because the condition it depends on is never updated.
Section 1 gave you values, names and operators, which is enough to compute a single result. What it left unfinished is everything that depends on the result. A metric you can compare against a threshold but cannot act on does not change what the program does, and a sweep over twenty hyperparameter values written as twenty copied lines is not a sweep, it is a transcription error waiting to happen.
Lessons
- Conditionals - if, elif, else, and Truthiness - branching on a condition, and which values count as true.
- The while Loop and the Accumulator Pattern - repeating until a condition changes, and building a result up across iterations.
- The for Loop, Iterables, and range - walking every item of a collection in order.
- Controlling Loops - break, continue, and Nesting - leaving early, skipping an iteration, and putting one loop inside another.
By the end you will be able to:
- Predict which branch of a conditional runs, including for non-boolean conditions.
- Write an accumulator loop that produces a correct running total or count.
- Choose between a
forloop and awhileloop from the problem statement alone. - Trace a nested loop's execution and say exactly how many times the inner body runs.