Python hash() Function – Tutorial with Examples

The hash() function in Python is a built-in function that returns the hash value of an object. The hash value is a numerical value that is used to identify an object in a collection, such as a list or a dictionary. The hash value is used to efficiently locate an object in the collection without having to search through all the elements of the collection. In Python, hashable objects are those that can be used as keys in dictionaries or elements in sets.

Syntax

hash(object)

Parameters

object – the object for which you want to obtain the hash value

Return Value

The hash() function returns the hash value of the object. The return value is an integer that is guaranteed to be unique for each object.

Examples

Example 1: Hashing an Integer

x = 10
print(hash(x))

Output:

10

In this example, the hash() function is used to obtain the hash value of an integer. The output shows that the hash value is equal to the value of the integer itself.

Example 2: Hashing a String

x = "Hello, World!"
print(hash(x))

Output:

-1219243545674798370

In this example, the hash() function is used to obtain the hash value of a string. The output shows that the hash value is a unique integer that is different from the value of the string itself.

Example 3: Hashing a List

x = [1, 2, 3, 4, 5]
print(hash(x))

Output:

TypeError: unhashable type: 'list'

In this example, the hash() function is used to obtain the hash value of a list. The output shows that a TypeError is raised because lists are mutable and therefore cannot be hashed. To use a list as a key in a dictionary or as an element in a set, it must first be converted to an immutable object, such as a tuple.

Use Cases

The hash() function has several important use cases, including:

  • Determining the uniqueness of an object in a collection: The hash value of an object can be used to determine whether the object is unique in a collection of objects. If two objects have the same hash value, they are considered to be equal and only one of them can be present in the collection.
  • Implementing efficient data structures: The hash value of an object can be used to implement efficient data structures, such as hash tables, which are used in dictionaries and sets. The hash value allows for constant time lookups and insertions in these data structures.
  • Hashing passwords: The hash value of a password can be used to store it securely in a database. The password is hashed and the resulting hash value is stored, instead of the plain text password. When the user logs in, the entered password is hashed and compared to the stored hash value. If they match, the user is authenticated.

In conclusion, the hash() function is a useful tool in Python that can be used to determine the uniqueness of an object and to implement efficient data structures. It is important to note that not all objects are hashable and only hashable objects can be used as keys in dictionaries or elements in sets.

Leave a Reply

Your email address will not be published. Required fields are marked *