How python hash() and hex() built-in function works?

Python hex() Function

Python hex() function is used to generate hex value of an integer argument. It takes an integer argument and returns an integer converted into a hexadecimal string. In case, we want to get a hexadecimal value of a float, then use float.hex() function.

Python hex() Function Example

# Calling function  
result = hex(1)   
# integer value  
result2 = hex(342)   
# Displaying result  
print(result)  
print(result2)  

Output:

0x1
0x156

Python hash() Function

Python hash() function is used to get the hash value of an object. Python calculates the hash value by using the hash algorithm. The hash values are integers and used to compare dictionary keys during a dictionary lookup. We can hash only the types which are given below:

Hashable types: * bool * int * long * float * string * Unicode * tuple * code object.

Python hash() Function Example

# Calling function  
result = hash(21) # integer value  
result2 = hash(22.2) # decimal value  
# Displaying result  
print(result)  
print(result2)  

Output:

21
461168601842737174