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.
The lessonsin the order to take them.
Glossaryevery term the module introduces.
Each 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
- The expression an
ifor awhileevaluates 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
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.
False,None,0,0.0and""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
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:. 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)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
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:
- ask for the loan amount in dollars, the interest rate as a percentage per month, and the monthly payment in dollars;
- keep money in whole cents, and each month add that month's interest, rounded to the cent;
- 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; - make the last payment only what is still owed, so the balance ends at
exactly
$0.00; - print the number of months, the total paid and the total interest at the end;
- 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.