Python · Module 2

Control Flow4 lessons and their Study toolkit.

Watch each lesson, answer its quiz, then do its practice. After the last lesson: the module quiz, the project, and the interview questions.

4of 4 lessons ready
35quiz questions
21key terms
3self-checking notebooks

The lessonsin the order to take them.

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

Glossaryevery term the module introduces.

Each 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
The expression an if or a while evaluates to True or False: total >= 50. 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. False, None, 0, 0.0 and "" count as False; every other value counts as True. 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 whose condition never becomes False, because nothing in the body changes what it reads. 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:. It keeps its last value after the loop. 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

Module quiz15 questions across it all.

Take it after the last lesson. Your first pick on each question is the one that counts.

Project: Loan Repayment Schedulebuild it without a template.

Stated as a problem, with no step-by-step instructions. Working out the steps is the point.

The task

Write a program, in one Colab notebook, that asks for a loan amount, a monthly interest rate and a monthly payment, and prints the repayment schedule. 03_module_project.ipynb checks the core of it (whether the loan is repaid, the number of months, the last payment and the total interest). Your program must:

  1. ask for the loan amount in dollars, the interest rate as a percentage per month, and the monthly payment in dollars;
  2. keep money in whole cents, and each month add that month's interest, rounded to the cent;
  3. print one line per month: the month number, the interest added, the payment and the balance left, each to the cent and with a $ sign;
  4. make the last payment only what is still owed, so the balance ends at exactly $0.00;
  5. print the number of months, the total paid and the total interest at the end;
  6. if the payment does not cover the first month's interest, print that the loan is never repaid, instead of looping for ever.

Notebooksthat check your answers.

Open them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.

  • Notebook 1 · ConditionsLessons 2.1 Courses plan
  • Notebook 2 · LoopsLessons 2.2 and 2.3 and 2.4 Courses plan
  • Notebook 3 · Project: Loan Repayment ScheduleThe module project, with checks Courses plan

Referencefor revising and for interviews.

The cheat sheet is one page of the module’s terms, rules and gotchas. The interview questions come with model answers.