Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is the hash function in Python?

Omu inetimi

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes. The values are usually used to index a fixed-size table called a hash table.

Hash functions are cryptographic functions that take in an arbitrary amount of data input and produce a fixed-size output of enciphered text called a hash value, or just “hash.”

Hashing differs from encryption in the sense that while encryption creates an indecipherable object from the plain text that can be decrypted from algorithms and a key, hashing is a one-way process. This means that once created, the original object cannot be retracted from the hash.

Python has a built-in hash() function that returns the hash value of an object. This makes creating the hash of an object quite simple in Python.

The Python hash function can be run on specific data types such as bool, int, long, float, string, Unicode, or tuple, but not on lists, sets, dictionaries, or byte arrays.

Syntax

>>> hash(object)
## returns the hash value of the object

Code

print(hash('this is an object'))
print(hash(('python','hash','function',1)))
#hash([1,2,3,4]) #It doesnt work for lists. Will throw a TypeError

RELATED TAGS

python

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring