EE1021 Lecture 4 Worksheet — Sequences, Tuples, and Lists
Topics: tuples and unpacking, list indexing and slicing, list methods (append, extend, insert, remove, pop), list comprehensions, aliasing and copying
Workflow: predict first, then run the code in VS Code, compare with your prediction, and explain any difference.
Core work: Tasks 1–7. Extension work: Shallow Copies.
Use the submission template EE1021_Lecture4_Sequences_Worksheet_Template.py. Keep the function and variable names it gives you.
- Unless a task says print, functions return their results.
- Do not use dictionaries yet; they come in Lecture 5.
1. Retrieval: Return and Unpack a Tuple
Write the function below. It must return a tuple containing the minimum and maximum readings.
def reading_limits(readings):
"""Return (minimum, maximum) for a non-empty list of readings."""Call it with [4.92, 5.08, 4.97, 5.11], unpack the result into low and high, and print:
Range: 4.92 to 5.11
In one comment, explain why the function returns one value even though two names receive the result.
2. Predict Indexing, Slicing, and Mutation
Without running the program, write the predicted output of all four print statements. Then run it and correct your prediction if needed.
values = [10, 20, 30, 40, 50]
print(values[-1])
print(values[1:4])
values[2] = 31
values[0:2] = [11, 21]
print(values)
print(len(values))In a comment, identify the two lines that mutate the list.
3. Choose the Correct List Operation
Start with:
lab_queue = ["group-01", "group-03", "group-04"]Use list operations to perform these steps in order:
- Insert group-02 immediately after group-01.
- Add group-05 to the end.
- Add both group-06 and group-07 as separate elements with one operation.
- Remove group-03 by value.
- Remove and store the front group in processed.
Print the queue after each step and print processed at the end. Do not rewrite the complete list manually.
4. Debug append Versus extend
The intended final list is [4.90, 4.95, 5.00, 5.05], but the program creates a nested list.
readings = [4.90, 4.95]
new_readings = [5.00, 5.05]
readings.append(new_readings)
print(readings)- Predict the incorrect output.
- Replace exactly one method name to fix the program.
- Explain why the original method created a nested list.
5. Build and Filter a List
Given:
millivolts = [4100, 4920, 5080, 5700, 4970]- Use a list comprehension to convert every value to volts.
- Use a second comprehension to keep only values from 4.75 V through 5.25 V, inclusive.
- Print both lists and the count of valid readings.
Do not modify millivolts.
6. Trace Aliasing and Cloning
Predict all three printed lists before running the program.
original = [1, 2]
alias = original
clone = original.copy()
alias.append(3)
clone.append(4)
print(original)
print(alias)
print(clone)Draw a small reference diagram or explain in two sentences:
- which names refer to the same list;
- why appending to clone does not append 4 to original.
7. Engineering Mini-Problem: Clean Sensor Readings
Implement:
def clean_readings(readings, low=4.75, high=5.25):
"""Return valid readings without changing the input list."""Requirements:
- Return a new list containing values in the inclusive range low through high.
- Use a list comprehension.
- Do not mutate readings.
- Test with raw = [4.10, 4.92, 5.08, 5.70, 4.97].
- Print raw after the call to demonstrate that it remains unchanged.
- Unpack the minimum and maximum valid readings and display the valid range.
Optional Extension: Shallow Copies
Predict the output and explain why copying the outer list does not copy the inner lists.
original = [[1, 2], [3, 4]]
clone = original.copy()
clone[0].append(99)
print(original)
print(clone)Submission Checklist
- All seven core tasks are complete.
- Every prediction is recorded before the corresponding actual result.
- No placeholder pass or None values remain.
- Functions return values instead of requesting input internally.
- The original list in Task 7 remains unchanged.