Review, explain, practice

Study guide: Work with Collections and Defensive Inputs

Given counts={"A":4,"B":7}, retrieve B and test whether 7 appears in[4,7]. Design a 0–10 integer input rule and explain why an alias is not an independent backup.

Download the grade study pack

Ideas to explain in your own words

Useful vocabulary

membership
Whether an item appears in a collection.
dictionary
Key-value associations.
mutation
Changing an existing object’s stored contents.

Watch your reasoning

A worked review example

Paper Python 3 expression: 3 in [1, 3, 5]. What value results? The supported answer is True. Tracing the expression under Python 3 rules gives True. Check whether the queried value exists anywhere in the supplied collection. A true membership result is not a numeric occurrence count. Membership asks whether a value appears in a collection. Trans form returns a new ordered list; source unchanged. Classify the stated result. The supported answer is New result, original retained. Compare the source before/after record and distinguish a new result from a mutation. Assigning another name to the same mutable object is not automatically an independent copy. The supplied facts, rather than an assumed real-world detail, determine this paper case. Compare the recorded action or value with the stated rule before choosing an answer.

Make a practice plan

  1. Make a four-row summary, one row for each lesson, with its computing idea and one worked decision.
  2. Explain one tempting mistake using the actual supplied example.
  3. Complete the new unit review before consulting its explanations.
  4. Try the transfer task on paper and compare your evidence against the model response.