/en-tutor-prep-s4-ict-python-dictionary-file-io/
/en-tutor-prep-s4-ict-python-dictionary-file-io/
英文版
3
General Inquiry: info@TutorZone.com.hk
Office Hours: Monday to Saturday - 9:00 AM to 6:00 PM
WhatsApp Monday to Sunday - 24 hours
Hotline: +852 6828 1809
Instant Support: +852 9061 3106

DSE ICT – Python Dictionary and File I/O Prep Guide (for tutors) | Key lookup, line-by-line reading, statistics, exceptions

DSE ICT – Python Dictionary and File I/O Prep Guide (for tutors) | Key lookup, line-by-line reading, statistics, exceptions

  • 2026-11-05

Students can open a file, then go quiet when asked why a dictionary beats two parallel lists. This lesson reads a five-student marks file, joins key lookup to line parsing and statistics, and uses a word count to show why a dictionary is the first choice for lookup.

Objectives — Students can: (1) read five rows of name and mark into a dictionary, giving a mean of 66.4, a top score of 91 for Cara and a low of 45 for Dan; (2) compute a pass rate of 80 percent and state when strip and split are needed while reading; (3) count word frequency with a dictionary, showing that to and be each appear twice in a six-word sentence, and explain why dictionary lookup beats scanning a list.

01 Prerequisites (3-minute check)

If any of these fails, fix it before the lesson starts.

Skill Quick check Pass mark
Input and output What is the difference between print and input? One outputs, the other waits for a value
Lists Which brackets does a list use? Square brackets, indexed from zero
Loops Which statement takes items one at a time in a for loop? for item in a list

02 Lesson flow (45 minutes)

Time Stage Tutor does Student does
0-5 min Prior knowledge check Ask the three prior questions and write the dictionary syntax on the board Answer orally and copy any wrong item once
5-17 min Act one: a key replaces two lists Build five names and marks into a dictionary and get the mean 66.4 Copy it, then add one more student
17-30 min Act two: read the file line by line Read six lines, skip the header, total 46 bytes Write the three reading lines and print each line
30-40 min Act three: statistics and exceptions Compute the pass rate 80 percent and handle a missing file Rewrite it with try
40-43 min One-sentence summary Ask for the advantage of a dictionary in lookup Answer orally; tutor corrects only wording
43-45 min Exit check Hand out three items and collect them Work alone with books closed

03 Teaching script (read it out)

Scene 1: Act one: one key for one value, no positions to remember

With two parallel lists you must remember which index holds the name and which holds the mark, and one insertion ruins both; a dictionary uses the name as a key and returns the mark directly.
5 students total 332 marks, a mean of 66.4; the top is Cara with 91 and the low is Dan with 45, reachable with max and min plus the key argument.
On lookups, a parallel list may compare up to 5 entries, while a dictionary averages 1, because the key is hashed straight to the slot.
One line to remember: find by name, use a dictionary; process in order, use a list.

Ask students to repeat: a dictionary locates by key, not by position.

Scene 2: Act two: a file is just lines of text

After open, each line is a string that still carries its newline, so strip first and split second.
The first of six lines is the header name,mark, which is skipped or consumed with next; the remaining 5 rows split into a name and a mark.
This small file is 46 bytes, which is each line length plus one newline; exam questions often ask for file size in bytes.
The sentence that matters: open with a with block so the file closes on exit and close is never forgotten.

Ask students to repeat: each line is a string, strip first, open with with.

Scene 3: Act three: statistics and errors are both part of the data

The pass mark is 50, counted with a condition: 4 of 5 pass, which is 80 percent.
Word frequency works the same way: split the sentence into 6 words, accumulate with get, so to and be each reach 2, the rest 1, and the dictionary holds 4 keys.
Real files have missing fields or non-numeric text, so wrap the conversion in try and log the line number in except instead of letting the program die.
In an answer, write the four steps: check, convert, summarise, output. The marker is reading the steps, not the polish.

Ask students to repeat: check before converting, and log the bad line rather than skipping it silently.

04 Question types and worked steps

Type Example Steps Answer
Build a dictionary 5 names and marks Write pairs directly, or zip two lists 5 keys in the dictionary
Read a file Six text lines with a header with open, strip, split, skip the header 5 records read
Statistics A list of marks Sum over count for the mean, condition count for the pass rate Mean 66.4, pass rate 80 percent
Exception handling Missing file or non-numeric field Wrap open and int in try except The program continues and prints the line number
Sanity check (teach this): Three read-backs: (1) the mean 66.4 must lie between the low 45 and the top 91, so anything outside means a wrong total; (2) a pass rate cannot exceed one hundred, and 4 over 5 gives 80 percent; (3) the dictionary of frequencies holds 4 keys, always fewer than the 6 words, and equal counts mean every word appeared once or the loop is wrong.

05 Independent practice (3 questions)

Question Answer What to watch
Five marks of 60, 70, 80, 90 and 100: what is the mean? 80 Check the divisor
A text file has seven lines and the first is a header: how many records? 6 records Check that the header was skipped
Why is dictionary lookup faster than a list scan? The key is hashed straight to the slot, so nothing is compared one by one Check that the key, not the position, is named

06 Common mistakes and what to say

Mistake Student writes Say this
Forgetting strip The mark field is ’72\n’ and int fails Strip before split; the newline is not data.
List and dictionary confused Names fetched by index, so one insertion shifts everything Look up by name and the dictionary key does it.
Wrong divisor Total 332 divided by 6 gives 55.3 There are only 5 records; the header is not a student.
Exception treated as fatal The program stops at the first bad row Use try except to log the line number and carry on.

07 Homework (paper, section and question numbers)

08 Exit check (last 5 minutes)

Question Answer Decision
5 students total 332 marks: what is the mean? 66.4 marks Error: dividing by 6
Why strip before converting? To remove the trailing newline, or the number conversion fails Error: answering for tidiness
What does a dictionary lookup use to locate a value? The key, hashed directly Error: answering the index order

09 Tutor FAQ

Which year is this for?

S4 ICT programming, and a starting point for S5 elective revision.

Is 45 minutes enough for file reading and exceptions?

Yes, since both use the same small file and one program is written once.

Is there a companion worksheet?

Three practice items with answers on the page, plus the Python basics chapter.

Find a tutor — freeTutor Registration →

A TutorZone lesson-prep guide. Written by the TutorZone team; questions and lesson flow are original and are not taken from any school or publisher material.

Join as a tutor or coach — academic, music, sports, arts, STEM. Free to register. See how it works →