liveBook · Manning

Key

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

A dictionary key is a hashable object used to identify and retrieve a value in a mapping.

A dictionary key is a hashable object that identifies a value in a mapping. Keys are unique, so assigning a second value to one replaces the first. section 4.1.3

Use keys to build and work with Python dictionaries:

  • Create entries with square brackets: answers['name'] = 'Sir Lancelot'. section 4.1.1
  • Retrieve a value with its key, or use get() when a missing key should return None or a supplied default instead of raising KeyError. section 4.1.2
  • Check membership with 'key' in dictionary, and inspect keys, values, or pairs with keys(), values(), and items(). section 4.1.2 section 4.1.3
  • Use strings, integers, or floats as keys; lists cannot serve because keys must be immutable. section 4.1.3

Indexing lines by their first letter

Build lookup = {} while reading a file, then store each line with lookup[line[0].upper()] = line.rstrip(). A later letter argument retrieves the matching line; an unknown value produces a helpful message. section 7.1 section 7.3.2

The Keeper’s first question is, “What is your name?” Lancelot answers, “My name is Sir Lancelot of Camelot.” We can add the key “name” to the answers dictionary by using square brackets ([]--not curlies!) and the literal string 'name':

>>> answers['name'] = 'Sir Lancelot'

Inspect a complete key assignment in the REPL before extending the dictionary with additional question-and-answer pairs. section 4.1.1

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

How deep it goes
Explains creation, lookup, membership, iteration, uniqueness, and immutability without descending into hashing internals.
Examples
Uses question-and-answer dictionaries, letter-indexed file lines, and unique word collection.
What it assumes
Assumes basic Python syntax and teaches dictionary construction, indexing, membership checks, and iteration directly.
What you can do after
Apply it via Python dictionaries to create lookups, retrieve values, detect missing entries, and remove duplicates.
How it connects
Connects keys with dictionaries, values, lists, strings, loops, and the get() method.

Also explained in

Compare how 10 books explain key →

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.