liveBook · Manning

Dict.get

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

dict.get is a Python dictionary method that returns the value for a key or a specified default when the key is absent.

dict.get retrieves a dictionary value without raising an exception when the key is absent. It returns None by default, or a caller-supplied default instead. section 4.4.3

Translate only mapped characters

A character-by-character translator can preserve unmapped characters with jumper.get(char, char): mapped digits change, while letters and punctuation remain unchanged. For example, '867-5309' becomes '243-0751'. section 4.4.3 section 4.2

Use dict.get when a lookup may miss:

  • jumper.get('A') returns None instead of raising the KeyError produced by jumper['A']. section 4.4.3
  • jumper.get('A', 'A') returns 'A', making the fallback explicit. section 4.4.3
  • lookup.get(letter.upper(), f'I do not know "{letter}".') combines normalized keys with a user-facing fallback. section 7.3.4
>>> text = '867-5309'
>>> [jumper.get(char, char) for char in text]
['2', '4', '3', '-', '0', '7', '5', '1']

Inspect each character's lookup result, including unchanged punctuation, before joining the results into a translated string. section 4.4.3

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Explains missing-key behavior, optional defaults, and use inside loops, but does not examine method internals.
Examples
Demonstrates digit substitution, preserved characters, lookup-table messages, list comprehensions, and joined translated strings.
What it assumes
Assumes familiarity with dictionaries and keys while reinforcing membership checks and safe retrieval.
What you can do after
Apply it via dictionary lookups to handle absent keys without exceptions and preserve or report missing values.
How it connects
Connects dictionary lookups with KeyError, for loops, string processing, and default values.

Also explained in

Compare how 5 books explain dict.get →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.