0% found this document useful (0 votes)
5 views7 pages

Python Hash Function Explained

A hash function converts input data into a fixed-size numerical value, known as a hash code, primarily used for fast data retrieval in data structures like hash tables and dictionaries. Python has a built-in hash() function, and good hash functions possess properties such as uniform distribution, determinism, and low collisions. Applications of hash functions include hash tables, password storage, data integrity, and cryptography.

Uploaded by

adeshgajare55
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Python Hash Function Explained

A hash function converts input data into a fixed-size numerical value, known as a hash code, primarily used for fast data retrieval in data structures like hash tables and dictionaries. Python has a built-in hash() function, and good hash functions possess properties such as uniform distribution, determinism, and low collisions. Applications of hash functions include hash tables, password storage, data integrity, and cryptography.

Uploaded by

adeshgajare55
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Hash Function in Python

Definition, Working, Properties, and


Examples
Definition
• A hash function is a function that converts
input data (key) into a fixed-size numerical
value called a hash code.
• It is mainly used in data structures like hash
tables, dictionaries, and sets for fast data
retrieval.
Working Principle
• 1. The key (like a name or number) is given as
input.
• 2. The hash function computes a numeric hash
value.
• 3. That value is used as an index in a hash
table to store or find the data.

• Example:
• Key → "apple"
Hash Function in Python
• Python provides a built-in function called
hash().
• Example:
• print(hash("hello"))
• print(hash(123))

• Used internally by:


• - dict (dictionary)
• - set
Properties of a Good Hash
Function
• • Uniform Distribution – Distributes keys
evenly
• • Deterministic – Same input gives same
output
• • Fast – Quick to compute
• • Low Collisions – Different keys produce
different hash values
• • Equality Consistency – If a == b → hash(a) ==
hash(b)
Example of a Simple Hash Function
• def simple_hash(key):
• h=0
• for ch in key:
• h += ord(ch)
• return h % 10 # table size = 10

• print(simple_hash("apple"))
• print(simple_hash("banana"))
Applications of Hash Functions
• • Hash Tables / Dictionaries
• • Password Storage (Encryption)
• • Data Integrity (Checksums)
• • Cryptography (SHA, MD5, etc.)

You might also like