Python · Module 2
Control Flow
Take each lesson, answer its quiz, then do its practice. After the last lesson come the module quiz, the project and the interview questions.
Lessons
Module material
Take these after the last lesson, in this order. Next on each page leads through them.
- Module quiz15 questions across the whole module. Take it after the last lesson; your first pick on each question is the one that counts.
- Project: Loan Repayment ScheduleStated as a problem, with no template and no step-by-step instructions. Working out the steps is the point.
- NotebooksOpen them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.
- Cheat sheetOne page of the module’s terms, rules and gotchas, with the output each rule produces.
- Interview questions25 questions interviewers ask on control flow, from first principles to the follow-ups. The first 3 model answers are free to read; Pro opens the rest.
Glossary
Every term the module introduces, with the lesson that introduces it.
- block
- The indented lines that belong to an
if,else,while,fororcase. The first line back at the outer level ends it. 2.1 - branch
- One of the blocks a condition chooses between. In an
ifandelse, exactly one branch runs. 2.1 - condition
- An expression whose truth value an
ifor awhiletests, such astotal >= 50orname. 2.1 - conditional expression
a if condition else b, an expression that gives one of two values:0.0 if total >= 50 else 4.99. 2.1- nesting
- A block inside another block, such as an
ifinside anif. Each level is indented four more spaces. 2.1 - truthiness
- Whether a value counts as True or False when it is used as a condition. Falsy built-in values include
False,None, zero of any numeric type, and empty strings or collections. Nonzero numbers and nonempty strings are truthy. 2.1 - assignment expression
name := value: assigns a value and gives that value, inside an expression.while (price := input("Price: ")) != "":reads and tests in one condition. Also called the walrus operator. 2.2- infinite loop
- A loop that never terminates. In 2.2's example, the condition stays True and there is no other exit. A
while Trueloop with a reachablebreakcan terminate (2.4). 2.2 - iteration
- One run of the loop body. 2.2
- loop
- Code that runs its body again and again: a
whilestatement or aforstatement, with its block. 2.2 - loop body
- The indented block that repeats. 2.2
- sentinel
- A value that marks the end of the input, and is not processed, such as an empty entry at a checkout. 2.2
- accumulator
- A name that collects a result across a loop's iterations. It is created before the loop (
total = 0), updated in the body (total += steps), and read after it. 2.3 - iterable
- Anything a
forloop can go through, one item at a time: a list, a string, arange. 2.3 - list
- Values in square brackets, in order, separated by commas:
[8200, 10450, 6300]. Module 2 uses lists only in loops; Python 3.1 teaches them. 2.3 - loop variable
- The name a
forloop binds to each item in turn:stepsinfor steps in week:. After the loop it retains the last value assigned to it; an empty loop does not bind it. 2.3 - nested loop
- A loop inside the body of another loop. The inner loop runs in full for every iteration of the outer one. 2.3
- range
- A sequence of whole numbers from a start, up to but not including a stop, with an optional step:
range(0, 20, 5)gives0,5,10,15. It works each number out when the loop asks for it. 2.3 - break
- Ends the loop at once; the program continues with the first line after it. It ends only the loop it is directly in. 2.4
- continue
- Ends this iteration; the loop goes on with the next item, or checks its condition again. 2.4
- loop else
- A block written at the same level as a
fororwhile, that runs when the loop ends withoutbreak. 2.4