Exercises
Practice reading access and signatures, tracing instances and hints, constructing records, and completing an explicit configuration-to-result workflow.
Q1. Distinguish three kinds of access
Which statement about these expressions is correct?
Select one choice, then check.
HintLook for the call marker
Parentheses invoke the selected callable. Brackets use a value as a key.
SolutionRead each operator literally
The first expression selects data, the second selects a method object, the third calls it, and the fourth performs keyed lookup.
Q2. Use an attribute failure as evidence
This program fails:
What is the best first response?
Select one choice, then check.
HintInspect the receiver
The exception says the selected object does not provide the requested attribute. First establish what that object is.
SolutionUse the supported Path attribute
Confirm that path is a Path, then use path.name for the final path
component. Brackets and a broad handler would hide the real interface.
Q3. Map a signature to a call
Consider this documented learning signature:
Path.read_text(encoding=None, errors=None, newline=None) -> str
In path.read_text(encoding="utf-8"), which parameter uses an explicit value,
and what happens to the other two parameters?
Select one choice, then check.
HintFollow the keyword name
A keyword argument states its parameter mapping directly.
SolutionUse one explicit option
encoding receives "utf-8". The call omits errors and newline, so
both retain None for this documented signature.
Q4. Choose an explicit library call
The documented learning signature is
numpy.linspace(start, stop, num=50). We need exactly five evenly spaced
values from 0.0 through 1.0, including both endpoints. Which call states
that choice most clearly?
Select one choice, then check.
HintOverride only the changed default
The interval endpoints are required. Name the optional count because five is a deliberate departure from its default.
SolutionName the sample count
Use np.linspace(0.0, 1.0, num=5). The result contains five evenly spaced
values, while the keyword makes the chosen count visible.
Q5. Trace instances and an alias
Predict the four printed values.
Answer it first, then check.
HintDraw two objects
same_celsius is another name for celsius. The fahrenheit constructor
call created a different instance.
SolutionKeep identity separate from equality
The output is 21.4, 70.3, °C, and °F. Mutation through the alias is
visible through celsius but does not reach the other instance.
Q6. Trace self through one call
Complete report_line so it reads fields from the instance that receives the
call. The two calls must produce different unit labels.
Editable Python
Ready to run.
HintRead through self
Inside the method, self is celsius for the first call and fahrenheit
for the second.
SolutionReturn fields from the receiver
Q7. Interpret a hinted boundary
What does this signature state?
Select one choice, then check.
HintRead the bars as alternatives
float | None permits either named value form. It does not add executable
checks to the function boundary.
SolutionTreat hints as interface notation
The parameter and return annotations describe expected alternatives. The body must still implement conversion, validation, and the all-unavailable result rule.
Q8. Read a frozen dataclass honestly
Which statement about this declaration is accurate?
Select one choice, then check.
HintKeep three claims separate
Ask what the dataclass generates, what frozen=True blocks, and what no
runtime validator was asked to do.
SolutionUse the narrow frozen guarantee
Initialization, display, and equality are generated. Ordinary field reassignment is blocked. Type validation and recursive immutability are not supplied by this declaration.
Q9. Separate configuration, input, and result
Which assignment of roles is correct for a measurement run?
Select one choice, then check.
HintUse time and ownership
Configuration is chosen before analysis. Input is observed data. Result is evidence computed from that data under the chosen settings.
SolutionKeep the run boundary explicit
Unit and thresholds belong to MeasurementConfig; the reading list remains
a separate input; counts and mean belong to MeasurementResult.
Q10. Complete the configuration-to-result flow
Complete analyze. Reject reversed or equal thresholds, treat None as one
rejected input, classify accepted readings with the supplied configuration,
and return a MeasurementResult. The same readings are analyzed with two
configurations so the changed setting remains visible.
Editable Python
Ready to run.
HintKeep one pass and three counters
After the threshold check, skip each None only after incrementing rejected.
For a number, append it to accepted and update exactly one class counter.