- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python hash() Function
The hash() function in Python returns the hash value of a specified object. For example:
a = 120 b = "fresherearth" c = "python programming" d = True e = False print(hash(a)) print(hash(b)) print(hash(c)) print(hash(d)) print(hash(e))
The output will be:
120 1095362836548201503 7646277759735715480 1 0
Note: Hashing in Python, is basically the process of converting any key or a string into another form (value).
Most of the time, hashing is used to represent a value by shorter and fixed-length value that becomes easier to find.
As you can see from above program and its sample output, both the string type object, produces hash value with fixed-length. Similar things goes with both d and e boolean type object.
Note: Since a hash value is basically a fixed-size integer, therefore for int type object, the hash() returns the same value as hash value too.
Python hash() Function Syntax
The syntax of hash() function in Python, is:
hash(obj)
where obj refers to the object whose hash value is to be calculated. Also the obj must be an immutable object.
Python hash() Function Example
The hash value is used to identify a particular value without caring the type of object. For example:
class fresherearth: Topic = "Python Programming" a = fresherearth.Topic b = "Python Programming" print(hash(a)) print(hash(b)) print(hash("Python Programming"))
The sample output produced by above program, is shown in the snapshot given below:
Important Use of hash in Python
Hash provides a quick look-up of values when dealing with some large collection of values. Set and Dictionary are the two examples. Since in case of a List, if we need to check whether a value is available in the list or not, using:
if x in mylist:
The whole list gets scanned one by one to check whether particular item or element or value is in the list or not. And of course, this may take longer time than usual when dealing with a list having high number of items. But in case of a set, Python keeps track of each hash. So that if we need to check whether an item is available or not using:
if x in myset:
Python will get the value of hash for x, to look that up in the internal structure to only compare x with myset that have same hash as x. This will work much faster than previous method.
Note: Similar things goes in case of dictionary. That is, in case of dictionary, the same things goes with keys of dictionary to look-up for particular key very fast.
It means that, list comes under the category of non-hashable object, unlike set and dict.
Non-hashable objects are objects that is mutable or changeable. That is, if items of an object can be changed, that means the objects comes under the category of non-hashable object.
Whereas hashable objects are objects that is immutable like set, keys of dict.
« Previous Function Next Function »