Chapter 3 — Programming foundations
Lesson map
Lesson 03 published. Map, example, EX007.
flowchart TD
N0["Learning outcomes"]
N1["3.1 A reporting summary"]
N2["Lab — Weekly reports by clinic and category"]
N3["3.2 Functions and loops"]
N4["3.3 Safe data handling"]
N5["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4 --> N5
N5 --> EX["Worked example"]
EX --> Q["Exercise EX007"]
Logical drawing
flowchart LR
N0["Learning outcomes"]
N1["3.1 A reporting summary"]
N2["Lab — Weekly reports by clinic and category"]
N3["3.2 Functions and loops"]
N4["3.3 Safe data handling"]
N5["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> Q["EX007"]
[Learning outcomes]
|
v
[3.1 A reporting summary]
|
v
[Lab — Weekly reports by clinic and category]
|
v
[3.2 Functions and loops]
|
v
[3.3 Safe data handling]
|
v
[Synthesis]
|
v
[Worked example] --> [Exercise EX007]
Learning outcomes
You will (1) write a Python script to summarise reported emails; (2) use functions and loops; (3) handle data safely.
3.1 A reporting summary
Brightwell's IT contractor forwards reported emails to a shared mailbox and exports a weekly list with date, clinic and subject category. Lucas writes a Python script that reads the list and counts reports by clinic and by category. He uses a loop to go through each row and a dictionary to store counts. The script prints a short summary for the weekly IT meeting, replacing a manual tally that took half an hour and often contained mistakes. The contractor reads it every Monday.
Lab — Weekly reports by clinic and category
# Weekly reports by clinic and category (synthetic export).
rows = [("Harbour", "fake invoice")] * 4 + [("Harbour", "fake login")] * 2 + [("Elm Road", "fake invoice")] * 3 + \
[("Elm Road", "other")] * 2 + [("Station", "fake login")] * 3 + [("Station", "fake invoice")] * 2 + [("Mill", "other")] * 1
def count(rows, i):
c = {}
for r in rows: c[r[i]] = c.get(r[i], 0) + 1
return dict(sorted(c.items(), key=lambda x: -x[1]))
print("by clinic:", count(rows, 0)); print("by category:", count(rows, 1))
Run with python3 lab_tally.py. Verified output:
by clinic: {'Harbour': 6, 'Elm Road': 5, 'Station': 5, 'Mill': 1}
by category: {'fake invoice': 9, 'fake login': 5, 'other': 3}
3.2 Functions and loops
Lucas organises his script into functions: one reads the data, one counts by a chosen field and one prints a table. Using one counting function for both clinics and categories avoids repeating code. He tests each function on a small sample he can check by hand. He learns that clear function names help others understand his code, which matters because the IT contractor will run it when Lucas is not there. He also adds short comments at the top of each function describing its input, its output and any assumptions it makes about the data.
3.3 Safe data handling
The export contains email subjects, which sometimes include patient names. Lucas changes the export to include only a category chosen by the reviewer rather than the full subject, so the script never handles patient information. He stores the file in a restricted folder and deletes old exports after the summary is made. Handling data safely is part of programming responsibly, especially in healthcare where patient confidentiality is a legal and ethical duty. The practice manager confirms the change with the data protection lead before the next export is produced.
Worked example
Lucas writes a tested Python tally with functions, removes patient names from the export and restricts storage.
Synthetic case. Data are synthetic.
Errors and limits
Categories depend on the reviewer. Scripts need maintenance.
Exercises
- EX007 — Manual tally problem. What problem did the script replace? Rubric: 3 = a slow, error-prone manual tally;; 2 = manual;; 1 = vague;; 0 = none.
- EX008 — Top category. Which category was most common in the lab? Rubric: 3 = fake invoice with 9;; 2 = invoices;; 1 = vague;; 0 = none.
- EX009 — Subject removal. Why remove email subjects from the export? Rubric: 3 = they can contain patient names;; 2 = privacy;; 1 = vague;; 0 = none.
Synthesis
Simple tested scripts save time when they are built to handle data safely.
Continue studying — chapter 03 of 30
← Chapter 02 (draft) · Course chapter index · Chapter 04 (draft) →