liveBook · Manning

Value

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

A value is the information associated with an attribute or dictionary key.

A value is the information associated with an attribute or dictionary key. In Python, a dictionary stores key/value pairs, such as a question answer or a descriptive phrase. section 4.1.1

Dictionary values support several everyday operations:

  • Assign or replace a value with square-bracket syntax; assigning the same key again leaves one entry containing the newest value. section 4.1.1 section 4.1.3
  • Inspect values directly with values(), or process keys and values together with items(). section 4.1.3
  • Use a value to represent the data retrieved for a known key, such as a phrase associated with its initial letter. section 7.1

Building a lookup dictionary

Read each input line, use line[0].upper() as its key, and store line.rstrip() as its value: lookup[line[0].upper()] = line.rstrip(). The resulting dictionary maps A to a complete phrase, B to another phrase, and so on; a requested letter retrieves its associated line. section 7.3.2 section 7.1

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'

A short REPL example shows assigning a string value to the name key. section 4.1.1

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

How deep it goes
Explains creation, replacement, iteration, and file-backed lookup, but does not descend into Python object internals.
Examples
Demonstrates answers for a quest, name, and color, plus phrases indexed by the first letters of input lines.
What you can do after
Build it from scratch by assigning, inspecting, iterating, and retrieving dictionary values in Python programs.
How it connects
Connects values with dictionaries, keys, assignment, iteration, file input, string methods, and lookup.

Also explained in

Compare how 7 books explain value →

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.