Lists and dictionaries: ordered items and key–value counts
Agent-assisted practice: write your own prediction and observed output first; only then may you ask an AI agent to explain or review, and you must record that assistance in your evidence.
Prerequisites
Day 08 completed, including its independent check. Recovery: Day 08 (loops and invariants), Day 07 activity 1 (mutation versus rebinding); Day 07 is the review day for this block.
Learning Objective
Create and update lists and dictionaries, check membership, read missing keys safely with get, and predict aliasing when two names refer to the same list, including empty and Unicode cases.
Instructions
Study source: within this activity's existing reading/practice time, use Automate the Boring Stuff with Python, 3rd edition, Chapter 6; Practice Questions; Appendix B answers, then explain one connection to the local task in your own words. Keep the local exercise and acceptance checks; use its original explanation if the external source is unavailable. Spaced-review warm-up: from memory, state the counting invariant from Day 08 and explain why a = [1]; b = a; a.append(2) changes b. You will see the same aliasing with lists today.
New ideas: list(text) gives a list of code points. A list keeps items in order and append adds to its end. A dictionary maps keys to values; d[key] = value adds or replaces, key in d checks for a key, and d.get(key, 0) returns 0 instead of failing when the key is missing. Dictionaries remember insertion order.
Predict every printed line, then run:
chars = list("ñu猫")
print(chars, len(chars))
print(len("n\u0303u"), "n\u0303u" == "ñu")
seen = []
for ch in "abca":
if ch not in seen:
seen.append(ch)
print(seen)
ages = {}
ages["猫"] = 3
ages["犬"] = 5
ages["猫"] = ages["猫"] + 1
print(ages, len(ages), "猫" in ages, ages.get("鳥", 0))
alias = seen
alias.append("z")
print(seen)
print(list(""), {})
Practice & Prediction Log
Write predictions for all six printed lines, then record the observed output. Explain why len("n\u0303u") is 3, why seen keeps only the first occurrence of each character, and why appending through alias also changes seen.
"ñ" here is one code point; "n" followed by a combining tilde (U+0303) is two. ages.get("鳥", 0) returns 0 because "鳥" is not a key. alias = seen does not copy the list; both names refer to one list object (Day 05).
Exact output:
['ñ', 'u', '猫'] 3
3 False
['a', 'b', 'c']
{'猫': 4, '犬': 5} 2 True 0
['a', 'b', 'c', 'z']
[] {}
list("ñu猫") has three code points. The decomposed "n\u0303u" has three code points and is not equal to the two-code-point "ñu". The loop appends a character only if it is not already in seen, so the result is ["a", "b", "c"]. Assigning to an existing key replaces its value, so "猫" becomes 4 and the dictionary still has two keys. alias and seen are the same list, so "z" appears in seen. The empty string gives an empty list, and {} is an empty dictionary.
All six lines are predicted before running, with each mismatch explained; you can explain get with a default, insertion order, the decomposed-character length, and list aliasing.
Authoritative Source Resources & Provenance
Automate the Boring Stuff with Python, 3rd edition
Relevant Section / Range: Chapter 6; Practice Questions; Appendix B answers
Verified Version & Date: Public edition checked 2026-11-01; moving pages may change
Beginner teaching and answer-keyed practice before checking exact semantics in the Python reference; selected chapters only, not the book's entire automation stack.
No prior programming required for Chapter 1; later chapters assume its basic Python concepts.
Use the original localized explanations, exercises and checks already present in this activity; no external account or purchase is required for that fallback.
Compared Candidate Sources & Selection Analysis (2)
Guided Python syntax but assumes programming familiarity; paired with the beginner text rather than used as its replacement.
Lectures and problem sets teach a broader CS1 course; the book offers shorter text-first entry and an answer appendix.
The Python Tutorial: More Control Flow Tools
Relevant Section / Range: Sections 4.2–4.10 (for, range, defining functions); data structures are in Chapter 5 of the same tutorial
Verified Version & Date: Python 3.12 documentation; verified 2026-10-30
Primary reference for Python iteration, functions, and containers.
Study Days 01-06 Python expression and filesystem fundamentals
The lesson explains all required behavior locally and can be completed offline.
Compared Candidate Sources & Selection Analysis (2)
Grammar-level definition of the same constructs; the tutorial chapter was selected for its guided pacing.
Lecture-paced coverage of the same control flow; deferred because the curriculum already drives the concepts through terminal drills.