Python: hash() function

hash() function

The hash() function returns 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).


(Python 3.2.5)




Name Description Required /
object An object. Required.

Example: Python hash() function

# hash for integer unchanged
print('Hash for 252 is:', hash(252))

# hash for decimal
print('Hash for 252.25 is:',hash(252.25))

# hash for string
print('Hash for Python is:', hash('Python Example'))


Hash for 252 is: 252
Hash for 252.25 is: 576460752303423740
Hash for Python is: 4427590093227766552

Python Code Editor:

Previous: globals()
Next: help()

Test your Python skills with w3resource's quiz

Python: Tips of the Day

How do I check if a list is empty?

For example, if passed the following:

a = []
if not a:
  print("List is empty")

Ref: https://bit.ly/2A4JXx9