In the new era of digital technology, Machine Learning, Artificial Intelligence and Cyber Security are a rising phenomenon. Python stands out as a language to implement much of the good sects of this.
hash() method to encode the data into unrecognisable value.
Syntax : hash(obj)
obj : The object which we need to convert into hash.
Returns : Returns the hashed value if possible.
Code #1 : Demonstrating working of hash()
The integer hash value is : 4 The string hash value is : -5570917502994512005 The float hash value is : 1291272085159665688
- Objects hashed using
hash()are irreversible, leading to loss of information.
hash()returns hashed value only for immutable objects, hence can be used as an indicator to check for mutable/immutable objects.
Code #2 : Demonstrating property of hash()
The tuple hash value is : 8315274433719620810
Traceback (most recent call last): File "/home/eb7e39084e3d151114ce5ed3e43babb8.py", line 15, in print ("The list hash value is : " + str(hash(list_val))) TypeError: unhashable type: 'list'
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course