Course materials · Microprocessor Applications

Python basics

English translation

Sensor records include missing and boundary values. Try handling normal and exceptional data with the same function.

Before practice

01 What happens to the mean if missing values are replaced with 0?

Description

This mixes actual 0 values with failed observations. First define the intended rule, such as exclusion or error handling.

02 What should a function return for empty data?

Description

Since the mean is undefined, choose a rule the caller can distinguish, such as an exception or None.

Practice

Preview practice notebookDownload practice notebook

1. Represent sensor records with data structures

Starting conditions

The record is [(0,21.0),(1,21.5),(2,None),(3,22.0),(4,20.5)], where each pair is time [s] and temperature [°C].

Procedure

  1. Convert each list item to a dictionary with time and temperature.
  2. Calculate the count, mean, minimum, and maximum of samples excluding None.
  3. Write a summarize function that leaves the original input unchanged.

Results to retain

Items to record and points to check
t [s]
Temperature [°C]
Validity

Points to check

  • Did you avoid interpreting None as 0°C?
  • Is the original list unchanged after the function call?

2. Boundary values and empty input

Starting conditions

Convert each of the three inputs in cases in the code to the same list of dictionaries as in Activity 1, then pass them to summarize. Leave the empty list unchanged, and map the first value of each pair to time and the second to temperature.

Procedure

  1. Define the behavior to return None for the mean when there are 0 valid samples.
  2. Check that the returned fields are the same for one sample and multiple samples.
  3. Use asserts to check empty input, input with missing values, and single-sample input separately.

Results to retain

Items to record and points to check
Input type
Valid count
Mean
Reason for handling

Points to check

  • Does it avoid division by 0 for 0 samples?
  • Do the returned fields stay the same regardless of input size?

3. Organize repeated measurements in a function

Starting conditions

Compare the two devices A=[212223]A=\begin{bmatrix}21&22&23\end{bmatrix} and B=[19,None,20,21]. The temperature unit is the same as in the original data.

Procedure

  1. Convert each device's temperature list to a list of dictionaries with time and temperature, and pass it to the same summarize function. Since these data have no times, use time=None and summarize only the temperatures.
  2. Record each device's valid sample count, mean, and range in one table.
  3. Briefly summarize what to check when comparing data with different sample counts and missing-value patterns.

Results to retain

Items to record and points to check
Device
Valid count
Mean [°C]
Minimum and maximum [°C]
Missing count

Points to check

  • Did you reuse a function instead of copying the same calculation code and modifying it separately?
  • Did you distinguish device differences from differences in sample composition?