EE1021 Programming for Engineers · Lecture 4 Lab · 2026–2027
By the end of the lab, you should be able to:
For every stage:
Open starter/lecture4_lab.py and keep its function names unchanged.
Answer without running Python:
Check: [1, 2, [3, 4]]; use extend; append mutates the list and returns None.
The starter contains:
Print:
Record the four predicted results in comments before running.
Perform these operations without rewriting the complete list:
Print the list after each operation. Identify which operations add one element and which can add several.
Show your partner, then the instructor if requested:
Complete:
def is_valid_voltage(value, low=4.75, high=5.25):
"""Return True when value is inside the inclusive valid range."""
passRequired boundary tests:
| Input | Expected |
|---|---|
| 4.74 | False |
| 4.75 | True |
| 5.25 | True |
| 5.26 | False |
Use is_valid_voltage inside a comprehension:
Requirements:
A student wrote:
Why does this produce Booleans instead of voltage values? Rewrite it so the expression produces each valid value and the condition filters.
Complete:
def summarize_readings(readings):
"""Return (minimum, maximum, average) for a non-empty list."""
passDo not print inside the function. In the main program:
Display the three results rounded to two decimal places.
Before using experimental data, verify a case with an obvious result:
Expected tuple: (1.0, 3.0, 2.0).
Explain why testing a small known case can reveal an error before the full workflow runs.
Pair each sensor ID with one reading using tuples:
sensor_ids = ["S1", "S2", "S3"]
readings = [4.92, 5.08, 4.97]
samples = []
for i in range(len(sensor_ids)):
samples.append((sensor_ids[i], readings[i]))Iterate over samples using tuple unpacking and print one report line per sensor.
Run one line at a time and record each prediction first.
Draw arrows from the two names to the list objects that exist after assignment.
Answer in comments:
Complete the starter so it:
Each student must complete one randomly selected action without partner help:
Use checkoff_rubric.qmd to see the criteria.
Submit three sentences or code fragments: