E ExamMaster

Engineering Python · Data & Control

Dicts and Lookup

In Engineering Python because configuration and labelled measurements are key→value tables — dict lookup is the everyday map.

A dict maps keys to values. Lookup by key is the point: names, ids, or labels as keys; readings or settings as values.

  • Engineering Python
  • Easy level
  • 4 concepts

1Building a dict

Curly braces with `key: value` pairs build a dict. Keys must be hashable (strings and numbers are common).

Figure. Curly braces with key: value pairs build a dict. sensor maps id to A1 and temp_c to 21.5. Keys must be hashable — strings and numbers are common.

Dict

sensor = {"id": "A1", "temp_c": 21.5}
print(sensor)
What does a dict store?
  1. Key→value pairs
  2. Only ordered numbers
  3. Only True/False

Dicts map keys to values.

2Lookup by key

`d[key]` returns the value or raises if missing. `.get(key, default)` returns a fallback instead of crashing.

Figure. cfg has rate 0.1 and epochs 5. cfg["rate"] returns 0.1. batch is missing, so .get("batch", 32) returns the fallback 32 instead of raising.

Takeaway

  1. Idea`d[key]` returns the value or raises if missing. `.get(key, default)` returns a fallback instead of crashing.

Lookup

cfg = {"rate": 0.1, "epochs": 5}
print(cfg["rate"])
print(cfg.get("batch", 32))
For prices = {"apple": 50, "banana": 20}, what is the safe way to access "cherry" without raising KeyError?
  1. prices["cherry"], which returns None automatically
  2. prices.find("cherry")
  3. prices.get("cherry", 0), providing a default fallback value
  4. prices.pop("cherry")

dict.get(key, default) safely returns the default if the key is absent, whereas square bracket lookup raises KeyError.

3Adding and updating keys

Assignment `d[key] = value` inserts or overwrites. Keys are unique — a second write replaces the first.

There is no second slot for the same key: later assignment is an update, not an append.

Figure. Assignment d[key] = value overwrites or inserts. epochs starts at 5 and becomes 10. batch is new and becomes 32. Keys stay unique — a second write replaces the first.

Update

cfg = {"epochs": 5}
cfg["epochs"] = 10
cfg["batch"] = 32
print(cfg)
After `d = {"a": 1}` then `d["a"] = 2`, what is `d["a"]`?
  1. 2
  2. 1
  3. [1, 2]

Assignment overwrites the value for that key.

4Lab: build and read a dict

Create a small settings dict and print one value the check can see.

Figure. settings maps units to cm and samples to 3. Lookup uses the key units, not a numeric position — the value the check can see is cm.

Takeaway

  1. IdeaCreate a small settings dict and print one value the check can see.

Coding lab. Dict lookup runs in the app, with checks on your output.

Notes

  • In Engineering Python because configuration and labelled measurements are key→value tables — dict lookup is the everyday map.
  • Curly braces with `key: value` pairs build a dict. Keys must be hashable (strings and numbers are common).
  • `d[key]` returns the value or raises if missing. `.get(key, default)` returns a fallback instead of crashing.

Exam traps & shortcuts

  • Run one cell at a time and read stdout before changing more lines.
  • Names are labels for values; rebinding a name does not rewrite old prints.

Recap

Dicts map keys to values; [] looks up; assignment inserts or replaces.

Building a dict
Curly braces with `key: value` pairs build a dict. Keys must be hashable (strings and numbers are common).
Lookup by key
`d[key]` returns the value or raises if missing. `.get(key, default)` returns a fallback instead of crashing.
Adding and updating keys
Assignment `d[key] = value` inserts or overwrites. Keys are unique — a second write replaces the first.
Lab: build and read a dict
Create a small settings dict and print one value the check can see.

Practise Dicts and Lookup

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • A 2-question practice set that ends the chapter
  • 3 quick checks with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.