SchoolWhool
Quizzes and saved progress, freeSign in freeSign in
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

  1. 2.1if, elif and elseMake the program choose.
  2. 2.2while LoopsRepeat until a condition changes, and avoid infinite loops.
  3. 2.3for Loops and rangeIterate over any sequence.
  4. 2.4break, continue and else on LoopsControl a loop from the inside.

Module material

Take these after the last lesson, in this order. Next on each page leads through them.

  1. Module quiz15 questions across the whole module. Take it after the last lesson; your first pick on each question is the one that counts.
  2. Project: Loan Repayment ScheduleStated as a problem, with no template and no step-by-step instructions. Working out the steps is the point.
  3. NotebooksOpen them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.
  4. Cheat sheetOne page of the module’s terms, rules and gotchas, with the output each rule produces.
  5. 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, for or case. The first line back at the outer level ends it. 2.1
branch
One of the blocks a condition chooses between. In an if and else, exactly one branch runs. 2.1
condition
An expression whose truth value an if or a while tests, such as total >= 50 or name. 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 if inside an if. 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 True loop with a reachable break can terminate (2.4). 2.2
iteration
One run of the loop body. 2.2
loop
Code that runs its body again and again: a while statement or a for statement, 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 for loop can go through, one item at a time: a list, a string, a range. 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 for loop binds to each item in turn: steps in for 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) gives 0, 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 for or while, that runs when the loop ends without break. 2.4