Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - HASH TABLE CODE




Example :



hashtable = {}

hashtable["ABA"] = 13
hashtable["CAB"] = 12
hashtable["BAC"] = 15

print(hashtable)

i = hashtable.get("ABA")

print("ABAs age is ",i)

j = hashtable.get("BAC")

print("BACs age is ",j)


Output :



  {'ABA': 13, 'CAB': 12, 'BAC': 15}
  ABAs age is 13
  BACs age is 15

Code explanation for Hash Table


Luckily, Python already provides Dictionary that represents HashTable.


As we can see we have stored the names ABA, CAB and BAC and their corresponding ages 13, 12 and 15 in the Hashtable.


hashtable["ABA"] = 13
hashtable["CAB"] = 12
hashtable["BAC"] = 15

But how they are stored in the HashTable will be decided by Python. As we are using the Dictionary provided by Python.


So, for ABA, a Hash Code is calculated by Python and the age of ABA is stored in some location decided by Python.


Similarly, the age of CAB and BAC is stored in the Hashtable after Python calculates the Hash Code, and decides where they will be stored.


After all the values are stored, we then check all the values stored in the Hashtable.


print(hashtable)

Then we try to retrieve the age of ABA.


i = hashtable.get("ABA")

So, we pass the name of ABA and internally Python calculates the Hash Code for ABA and gives us the age of ABA from the Hashtable.


And we print the age of ABA on the screen.


print("ABAs age is ",i)

Similarly we retrieve the age of BAC and print it on the screen.


j = hashtable.get("BAC")

print("BACs age is ",j)