What is hash() in Python?

The hash() method returns the hash value of an object in Python.

Syntax

The syntax of this function is as follows:

hash(object)

Parameters and return value

The hash() function takes one mandatory parameter, which is the object whose hash value is required.

The return value is an integer indicating the hash value of the specified object.

Example

In the example below, the hash() method is used to generate the has hash values of a string, integer, decimal number, and a set.

# hash value for string
print('The hash for Educative is:', hash('Educative'))
# hash value for integer
print('The hash for 470 is:', hash(181))
# hash value for decimal numbers
print('The hash for 156.2 is:',hash(181.23))
# hash value for list
print('The hash for (1,2,3) is:', hash((1,2,3)))

Free Resources