liveBook · Manning

Name-value pair

As explained in Get Programming: Learn to code with Python

A name-value pair is a data association in which an identifying name or key corresponds to a value.

A name-value pair links an identifying key to its associated value. In a Python dictionary, each entry is one pair: use the key to look up the value. For example, a grocery item can be the key and its quantity the value. section 27.1

grocery = {"milk": 1, "eggs": 12, "bread": 2}

Each grocery item to the left of a colon is a key; the quantity to its right is the value. section 27.1

Pairs can change as a dictionary is updated:

  • Add a pair by assigning a value to a key, as in d[k] = v. section 27.2
  • Assigning a value to a key already in the dictionary replaces its previous value; keys are unique. section 27.2
  • Remove a pair with d.pop(k); this removes the entry and returns its value. section 27.3

How Get Programming: Learn to code with Python covers it

How deep it goes
Defines keys and values, then demonstrates creating, adding, replacing, and removing dictionary entries.
Examples
Uses grocery quantities, animal leg counts, and household contents as dictionary entries.
What you can do after
Operate Python dictionaries by adding, replacing, and removing key-value pairs.
How it connects
Connects name-value pairs to Python dictionaries, keys, values, tuples, and hash functions.

Also explained in

Compare how 11 books explain name-value pair →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.