89DEVs

Python: hash() function

hash() function overview

The hash() function in Python creates a hash for a given object. The hash value is used to see if a given object has changed. Hash values are used in blockchains such as bitcoin.

hash() on string

The hash() function can be used to create a hash of the value of a given string. This is useful to compare different strings with each other and to detect if a string has changed. # hash() function used on python string myString = 'hello world' myHash = hash(myString) print(myHash) In this example, the value 'hello world' is assigned to a variable with the name myString. Then, the hash() function is used and the string is passed as argument. The hash is then assigned to the variable myHash. Finally, the result is printed and shown below. -4898812209996156682 How to use the hash function to see if a value has changed? We know that the string 'hello world' produces a hash value of -4898812209996156682. If any character in the string is changed, the hash value will also change. However if nothing is changed the hash value will stay the same. We can therefore use the hash values to check for changes as shown below. # check if string has changed using the hash() function myString = 'helloworld' if hash(myString) == -4898812209996156682: print('no change') else: print('change') In this example a slightly modified string is assigned to the variable myString. In fact just the space between hello and world was removed. We then compare the hash of the variable with the known hash value of 'hello world' from the example above. As expected the else statement is executed and 'change' is printed as shown below. change This short example, uses the fact that the hash values of 'hello world' and 'helloworld' are different. The example is quite trivial, however it becomes more useful if we want to detect changes in longer texts. In fact the same principle is also used in blockchains like bitcoin to check if previous blocks have been changed or not.

hash() for integers

The hash() function can be used for integers in the same way it's used for strings. It is also possible to detect changes in integers using the hash() function. # hash() function for integers myInt = 5 myHash = hash(myInt) print(myHash) In this example the hash value of the integer is assigned to myHash and then the value is printed. 5 As we can see the hash value of an integer is the integer itself. Therefore the use-case for integers is limited.

hash() function for float values

The hash() function can be used for float values as shown below. # hash function on float values myFloat = 2.555 myHash = hash(myFloat) print(myHash) Here, the float value is assigned to myFloat. Then, the hash value of myFloat is assigned to myHash. Finally, the hash value is printed and the result is shown below. 1279742870113600514 The hash() function can be used to detect changes in float values in the same way it's used for strings. For example the float values 2.01 and 2.02 have different hash values. However, it is important to understand that the float value 2.00 for example will be interpreted as integer. Therefore the hash value in this case is 2.

hash() on tuples

The hash() function can be used on immutable objects as tuples. It can not be used on mutable objects like lists, sets or dictionaries. # hash() function on tuples myTuple = (1, 2, 3) myHash = hash(myTuple) print(myHash) In this example the hash value of myTuple is assigned to myHash and then printed as shown below. 529344067295497451 The hash() function is useful to see if a new tuple was assigned to a variable. For example the tuple(1, 2, 3, 4) has a completely different hash value as shown below. 590899387183067792

hash() syntax

The syntax of the hash() function is: hash(object)

hash() arguments

The hash() function takes exactly one argument. If more or less than one argument are given, a TypeError exception is raised. The single argument for the hash() function can be of type string integer or float. The hash() function also works on immutable objects as a tuple.

hash() return value

The return value of the hash() function is the hash value of the given object. The hash() value is unique for the given object and can be used to compare changes in the given object.

                
        

Summary


Click to jump to section