An engineer stores temperature readings in degrees Celsius and uses this Python function to add a reading and calculate deviations from the updated mean:
def record_and_center(readings, new_value): readings.append(new_value) mean = sum(readings) / len(readings) centered = [value - mean for value in readings] return mean, centered samples = [18.0, 22.0] average, deviations = record_and_center(samples, 24.5) deviations[0] = -2.0
After every statement has executed, report average, samples[0], len(samples), and deviations[2]. Give the average, first sample, and last deviation in degrees Celsius to one decimal place. Give the number of samples as an exact integer. Python list indices start at zero.
Unlock full course notes by signing up
Recognize a keyword-only interface
An engineer writes this Python function to calculate pressure from force in newtons and area in square metres:
Complete the statements about how its interface controls the following calls. Use the standard Python terminology and exact exception name.
In pressure(240.0, area_m2=0.006), the value for area_m2 is supplied as a ________. In contrast, pressure(240.0, 0.006) raises ________ before the function body executes.
Pass a calculation rule into a function
A numerical comparison tool uses the following Python functions. All inputs and outputs in this example are dimensionless.
Each table row represents a call to output_change with the listed function and numerical arguments. Complete the returned changes as exact integers. A function name without parentheses passes the function itself as an argument.
Separate local adjustment from caller state
A force sensor correction subtracts a supplied zero offset plus an additional 2.0 N calibration adjustment. Consider this Python 3 code:
After both calls finish, what are the values of first_n, second_n, and offset_n, respectively? All three values are in newtons.
Audit the complete engineering function contract
An engineering function has this Python 3 signature:
Its contract requires nonnegative flow in litres per second, nonnegative pressure increase in kilopascals, and an efficiency satisfying 0<η≤10<\eta\leq10<η≤1. All inputs are finite real numbers. Any input outside this domain must raise ValueError; otherwise, the function must return input power in kilowatts.
Use