- Explain structured data and persistent files in plain language.
- Trace information from input to output.
- Test one subsystem independently.
- Recognize a common failure and respond safely.
- Modify the example without changing several variables at once.
The central idea: structured data and persistent files
Lists preserve an ordered sequence, dictionaries associate keys with values, and files preserve serialized data after the program ends. Choose a structure based on how the program needs to retrieve and update information.
Treat the system as a chain of observable inputs, explicit processing, a decision, and an output. That makes the design easier to explain and gives you a place to test when something fails. A working result is not enough: record what input produced it and what evidence shows the result is correct.
Follow the information through the system
Read the diagram from left to right. Each arrow is a boundary where data, electrical signals, or commands move between parts. Test those boundaries separately before combining the whole system.
A sensor log keeps each reading as a dictionary and stores many reading dictionaries in a list before writing rows to a CSV file.
- Use a list for ordered repeated items.
- Use a dictionary for named fields.
- Open text files with an explicit encoding and a context manager.
dictionary reading → list of readings → CSV file → later programA small working example
Run this example exactly as shown before modifying it. Predict the output first, compare that prediction with the real result, and change only one part at a time.
readings = [{"sensor": "light", "value": 42}, {"sensor": "light", "value": 47}]
with open("readings.txt", "w", encoding="utf-8") as file:
for item in readings:
file.write(f"{item['value']}\n")The context manager closes the file even if later code raises an error.
Expected result: A readings.txt file containing 42 and 47 on separate lines.
Test before you trust the result
Start with a normal case, then test a boundary case and a failure case. For software, try empty, unexpected, and extreme inputs. For hardware, begin with wheels raised or actuators disconnected, keep a physical power switch within reach, and verify safe stopping behavior.
Write down the expected result before each test. If the observation differs, stop and find the first boundary where the expected signal disappeared. This method is slower than changing many things at once, but it produces evidence instead of guesses.
- Test one input with a known value.
- Observe the intermediate value before the final action.
- Confirm a safe response when input is missing or invalid.
- Repeat the same test to check consistency.
Important terms
- List
- An ordered mutable collection.
- Dictionary
- A mapping from unique keys to values.
- Key
- The lookup name in a dictionary.
- File path
- A location identifying a file.
- Context manager
- A construct that reliably closes a resource after use.
Mini project: practice structured data and persistent files
- Create two reading dictionaries.
- Put them in a list.
- Write one value per line to a text file.
- Read the file back.
- Compare the loaded text with the source values.
Common mistakes and debugging
- Assuming a missing dictionary key exists.
- Confusing list positions with dictionary keys.
- Forgetting that file input is text.
- Using an ambiguous working-directory path.
Independent challenge
Store three study sessions with topic and minutes, write them to CSV with the csv module, then read them and calculate total minutes.
Check your understanding: 10 questions
What is the main purpose of structured data and persistent files?
What does “List” mean in this lesson?
How is “Dictionary” used?
Why should you test one boundary or subsystem at a time?
What should the system do when an input is missing or invalid?
In your own words, what does “List” mean?
In your own words, what does “Dictionary” mean?
In your own words, what does “Key” mean?
In your own words, what does “File path” mean?
In your own words, what does “Context manager” mean?
Quiz answers
Reveal all 10 answers after your attempt
- Lists preserve an ordered sequence, dictionaries associate keys with values, and files preserve serialized data after the program ends. Choose a structure based on how the program needs to retrieve and update information.
- An ordered mutable collection.
- A mapping from unique keys to values.
- It reveals where the expected value or behavior first changes, so debugging is based on evidence.
- It should reject the input, report the problem, or move to a documented safe state instead of guessing.
- An ordered mutable collection.
- A mapping from unique keys to values.
- The lookup name in a dictionary.
- A location identifying a file.
- A construct that reliably closes a resource after use.
Summary
structured data and persistent files becomes manageable when you identify the input, processing, decision, output, and evidence of success. Build the smallest version, test it, then make one measured change.
Continue learning
Continue to the next lesson, which builds on this model with one additional capability.
Choose a connected learning pathSources and further reading
Prepared 2026-10-02. Editorially completed for the course. Software examples are designed for current Python 3; verify board, operating-system, library, and hardware details against the linked primary documentation before a physical build.