liveBook · Manning

Hash function

As explained in Python Workout, Second Edition

A hash function is a function that maps a hashable Python object to an integer hash value used by hash-based data structures.

A hash function maps a hashable Python object to an integer hash value. Python uses hashable objects as dictionary keys; the hash supports key uniqueness and fast lookup. section 5.1

Think of hashing as directing a lookup to a numbered location. Searching a string, list, or tuple may inspect values one by one, taking O(n) time; a dictionary can use the hash to target a location in O(1) time as the collection grows. section 5.1 section 5.1

Numbered-office lookup

Assign each last-name initial to a numbered office: A maps to 1, B to 2, and S to 19. A visitor looking for Ms. Smith calculates 19 and goes directly there instead of checking every office. section 5.1

How Python Workout, Second Edition covers it

How deep it goes
Explains hash-based lookup through an office analogy and contrasts it with linear sequence search, without detailing collision handling.
Examples
Uses numbered offices and surname initials to illustrate direct lookup compared with checking offices sequentially.
What it assumes
Assumes familiarity with Python objects, dictionaries, sequences, and basic O(n) and O(1) complexity notation.
What you can do after
Evaluate why dictionaries require hashable keys and why their lookups can outperform searches through strings, lists, or tuples.
How it connects
Connects hash functions with hashable types, dictionaries, dictionary keys, and sequence-search complexity.

Also explained in

Compare how 16 books explain hash function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.