Python hash() built-in function
From the Python 3 documentation
Return the hash value of the object (if it has one). Hash values are integers. They are used to quickly compare dictionary keys during a dictionary lookup. Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0).
Introduction
The hash()
function returns an integer representing the hash value of an object. This is primarily used by dictionaries to quickly look up keys.
Only “hashable” objects can be passed to hash()
. An object is hashable if it has a hash value that never changes during its lifetime. All of Python’s built-in immutable types (like strings, numbers, and tuples) are hashable, while mutable containers (like lists and dictionaries) are not.
Examples
# Hash of an integer is the integer itself
print(hash(1)) # Output: 1
print(hash(1.0)) # Output: 1 (1 and 1.0 are equal)
# Hash of a string (output varies)
print(hash('hello'))
# Hash of a tuple (output varies)
print(hash((1, 2, 3)))
# Trying to hash a list will raise a TypeError
try:
hash([1, 2, 3])
except TypeError as e:
print(e)
# Output: unhashable type: 'list'