Python Fundamentals · Python Runtime and Core Syntax
26 cards
Control Flow and Patterns
-
Quick check
Which description of an `if` chain is correct?
AIt may carry any number of `elif` clauses, and the final `else` is optional
Right. There can be zero or more `elif` parts, and the `else` part is optional, so a bare `if` is a complete statement.
BIt must always finish with an `elif`, even when no alternative path is wanted
A chain can stop after its first condition; nothing obliges it to end with an alternative clause.
CIt can repeat `else` as often as needed, but it cannot contain any `elif`
A chain has at most one `else`, and `elif` is precisely the clause used for further ordered conditions.
2 / 26
-
Quick check
How does a `for` statement move through a list or a string?
AIt builds an arithmetic progression from the length of the sequence
Building a progression of numbers is the job of `range()`, which is a separate tool from the `for` statement itself.
BIt takes the items of the sequence in the order in which they appear
Right. A `for` loop iterates over the items of a sequence in their existing order, with no counter to maintain.
CIt re-tests a condition on each pass and never asks for an item
Re-testing a condition describes a `while` loop; a `for` loop obtains successive items instead.
5 / 26
-
Quick check
Which values does `range(0, 10, 3)` produce?
A`0, 3, 6, 9, 10`, since the end point closes the progression
The end point is never part of the generated sequence, so ten cannot appear among the values.
B`3, 6, 9, 12`, since the step also sets the first value
The progression starts at the start value, which is zero here; the step only sets the increment.
C`0, 3, 6, 9`, since the end point is never generated
Right. It begins at zero, advances by three, and stops before the excluded end point of ten.
8 / 26
-
Keep your progress in the app
That’s 3 of 10 quick checks. In the app they stay answered, and every lesson remembers where you left off.
-
Quick check
A nested search must stop testing candidates for the current record once it finds a match, yet keep processing later records. Where should the exit go?
A`continue` in the inner candidate loop, which ends the candidate search
`continue` abandons only the current pass and lets the candidate loop carry on testing, which is not the requirement.
B`pass` in the inner candidate loop, which moves both loops along at once
`pass` performs no action at all, so the candidate loop would simply run to its end.
C`break` in the inner candidate loop, so the outer record loop keeps running
Right. `break` exits the innermost enclosing loop, so placing it in the candidate loop ends only that search and leaves the outer loop free to continue.
10 / 26
-
Quick check
A loop body executes `continue`. What happens next?
AEvery loop currently running is terminated straight away
Not even one loop is terminated; `continue` never ends a loop, and no statement ends several at once.
BThe loop moves on to its next iteration
Right. `continue` skips the remainder of the current pass and proceeds with the next iteration.
CThe loop's `else` clause runs immediately
A loop `else` runs after the loop finishes without a `break`, not in the middle of an iteration.
12 / 26
-
Quick check
When does an `else` clause attached to a `while` loop run?
AOnly when a `break` has ended the loop ahead of time
A `break` is precisely what suppresses the `else` clause, along with a `return` or a raised exception.
BAt the end of every single pass whose condition happened to test true
The clause runs once, after the loop is over, not after each individual pass.
COnce the condition turns false, provided no `break` ended the loop
Right. For a `while` loop, finishing normally means the condition became false, and no `break` may have intervened.
15 / 26
-
Quick check
Why put `pass` in a function body that has not been written yet?
AIt fills a position where a statement is required, while doing nothing
Right. `pass` performs no action and exists to satisfy a place where Python's syntax requires a statement.
BIt hands back `True` so the caller can carry on for the moment
`pass` produces no value at all; a function left this way simply has an empty body.
CIt jumps to the next iteration of whatever loop encloses the call
Advancing a loop is what `continue` does, and `pass` has no effect on control flow.
17 / 26
-
Quick check
A `match` statement is given a subject that no `case` pattern matches, and there is no wildcard case. What runs?
AThe last `case` block, which acts as a fallback when nothing else fits
A final case is only a fallback when its pattern is the standalone `_` wildcard, which always matches.
BNo branch at all, because a subject that matches nothing selects nothing
Right. If no case matches, none of the branches is executed.
CEvery `case` block in turn, since the subject is compared against all of them
Only the first matching pattern is executed; the statement never runs several branches.
20 / 26
-
Quick check
A routing rule receives a dictionary that may hold extra keys. It must capture `bandwidth` and `latency`, accept the case only when latency is under a threshold, and otherwise fall through to the next case. Which design fits?
AA sequence pattern for the two keys, using `_` as a guard that rejects extra keys
Sequence patterns do not match dictionaries, and `_` is a wildcard pattern rather than a guard.
BA mapping pattern for the two keys, with an `if` guard on the captured latency
Right. A mapping pattern captures the requested values and ignores extra keys, and a false guard moves matching on to the next case.
CTwo dotted constant patterns, so the values are bound before any case is chosen
Dotted names identify named constants to compare against; they capture nothing and cannot express the threshold test.
24 / 26
-
Quick check
A search must report `not found` only after examining every item, and stay silent when a match ends the search early. Which structure does that?
APut the report in the loop's `else` clause and `break` when a match is found
Right. The loop's `else` runs only when the loop finished without a `break`, so a matching `break` cleanly separates found from not-found.
BPut the report just after the matching `if` and `continue` when a match is found
`continue` never ends the loop, so the report placed there would run on every pass that found nothing.
CPut the report before the loop starts and `break` once the first item is examined
A report written before the loop runs before anything has been examined, so it can say nothing about the outcome.
26 / 26
-
10 quick checks · then the test
In the app, finishing the quick checks opens this lesson’s 10-question test, and the ones you miss come back exactly when you’re about to forget them.
The whole course, on your phone
Lessons you can read, audio you can listen to on the way to work, and practice that remembers what you got wrong.