Python Fundamentals · Python Runtime and Core Syntax
32 cards
Values, Text, and First Programs
-
Quick check
Which operator always produces a floating-point result, even when the division is exact?
A`%`, because a remainder is measured against a fractional divisor
`%` returns the remainder of a division, and with two integer operands that remainder is an integer.
B`**`, because raising to a power widens the value
`**` calculates a power; with integer operands such as `5 ** 2` the result is the integer `25`.
C`/`, because regular division returns a float in every case
Right. Regular division always returns a float, which is why `(50 - 5 * 6) / 4` shows `5.0` rather than `5`.
4 / 32
-
Quick check
A program needs the exact text `C:\this\name`, with both backslashes preserved, and the value does not end in a backslash. Which literal fits?
AThe raw literal `r'C:\this\name'`, which leaves the backslash sequences alone
Right. A raw string does not interpret backslash-prefixed characters, and this value does not end in a backslash, so the restriction never applies.
BThe plain literal `'C:\this\name'`, written with no prefix at all
Without the raw prefix, `\t` and `\n` are read as a tab and a newline, so the backslashes are lost from the value.
CThe raw literal `r'C:\this\name\'`, trimming the final backslash afterwards
A raw string may not end in an odd number of backslashes, so that literal is not usable in the first place.
8 / 32
-
Quick check
`prefix` holds `'Py'`. Which expression correctly produces `'Python'`?
A`prefix 'thon'`, since neighbouring pieces of text are always joined for you
Automatic joining happens only between two literals; with a variable on the left this is a syntax error.
B`prefix + 'thon'`, since a variable has to be joined with the plus operator
Right. Adjacent literals concatenate on their own, but a variable or expression must be joined explicitly with `+`.
C`prefix * 'thon'`, since the repetition operator glues the two parts together
Multiplication repeats a string a whole number of times; it cannot combine two pieces of text.
10 / 32
-
Keep your progress in the app
That’s 3 of 11 quick checks. In the app they stay answered, and every lesson remembers where you left off.
-
Quick check
For `word = 'Python'`, what does `word[2:5]` produce?
A`'thon'`, because both boundaries are included in the result
Including both edges would return four characters; the end boundary of a slice is never part of the result.
B`'ho'`, because a slice drops the characters at both boundaries
Only the end boundary is excluded; the character at the start position is always kept.
C`'tho'`, because position 2 is included and position 5 is excluded
Right. A slice runs from its included start edge up to, but not including, its end edge.
13 / 32
-
Quick check
Code assigns a new character to one indexed position of a string. What happens?
APython raises `TypeError`, because a string cannot be changed in place
Right. Strings are immutable, so item assignment fails; a new string has to be built from slices and text instead.
BPython appends the new character at the end of the existing string
Appending is not what indexed assignment requests, and it would not be possible on an immutable value either.
CPython swaps the character in place, since a string is a mutable sequence
Strings cannot be modified after they are created, which is exactly why the assignment fails.
15 / 32
-
Quick check
Which operation adds exactly one new item at the end of an existing list?
ATaking a slice that covers the whole list
A full slice returns a new list with the same items; it adds nothing to the original.
BCalling the list's `append()` method
Right. `append()` mutates the list by placing one new item at its end.
CBinding the list to a second variable name
Assignment gives the same list another name; it changes neither the items nor the length.
18 / 32
-
Quick check
After `rgba = rgb`, an item is appended through `rgba`. What does `rgb` show?
AOnly `rgb` changes, because the original name owns every change made
Neither name owns the list; both simply refer to it, so the change is not attached to one of them.
BNothing changes anywhere at all, because the items of a list cannot be modified
Lists are mutable, and `append()` genuinely adds an item to the shared list.
CThe appended item too, because both names refer to one and the same list
Right. Simple assignment does not copy, so the two names refer to one list and both see the appended item.
20 / 32
-
Quick check
A countdown must repeat while a positive integer remains and print each value followed by a comma instead of a line break. Which design does that?
AUse an empty list as the `while` condition and call `print(value, end=' ')`
An empty sequence is false, so that loop would never run a single iteration.
BUse the integer as the `while` condition and call `print(value, end=',')`
Right. A nonzero integer is true until it reaches zero, and the `end` keyword replaces the newline with the requested comma.
CUse the integer as the `while` condition and write the value at the prompt
Echoing an expression only shows values at the interactive prompt, and it offers no control over what ends the line.
24 / 32
-
Quick check
Why can Python show `0.1` when the value stored for it is not exactly one tenth?
ABecause the stored value becomes exact one tenth when printed
Printing never rewrites the stored value; only the text on screen is chosen for you.
BBecause every decimal fraction is stored as a finite base-two fraction already
Most decimal fractions have no exact finite binary form, which is the whole reason the approximation exists.
CBecause what you see is a rounded decimal form of the stored binary approximation
Right. Python displays a short rounded form of the nearest representable binary fraction it actually holds.
27 / 32
-
Quick check
A report must show a short decimal result, while a test must tolerate the representation error in `0.1 + 0.1 + 0.1`. What handles both?
ARound each operand first and then compare with `==`, since the inputs become exact
Pre-rounding cannot bring either value closer to its exact decimal, so the strict comparison still fails.
BFormat the report output, and compare with `math.isclose()` in the test
Right. Formatting controls what the report displays, and `math.isclose()` is the tool for comparing inexact results.
CPrint every stored digit and compare with `==`, since no error is hidden
Showing more digits reveals the approximation but does not remove it, so strict equality still fails.
30 / 32
-
Quick check
How does Python process `a, b = b, a + b`?
AIt evaluates both right-hand expressions before it assigns either name
Right. Every right-hand expression is evaluated first, from left to right, before any name is updated.
BIt assigns `a` first, then evaluates the expression for `b`
Assigning `a` first would destroy the old value that the second expression still needs.
CIt works from right to left, assigning each name as soon as its value appears
No assignment happens part-way through; the complete right-hand side is evaluated before any name changes.
32 / 32
-
11 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.