hash() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

hash() function in python is used to return a hashed integer value of the object we pass as a parameter into it iff the object is hashable. Generally, the hash values are used to compare the dictionary keys while doing a dictionary lookup.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Syntax of hash() function in Python

Syntax for using the hash() function is as follows:

Parameters of hash() function in Python

hash() function takes one parameter as an input:

  • object: an object whose hash value is to be returned. An object can be an integer, string, list, tuple, etc.

Return Values of hash() function in Python

Return Type: <class 'int'>

  • hash() function returns an integer value which is the hash of the object passed into the function.
Sharpen Your Fundamentals with Free Learning

Example of hash() function in Python

Code:

Output:

What is hash() function in Python?

hash() function is used in Python to obtain the object's hash value. The object can be of integer, string, list, tuple type. The integer value is returned from the hash function, which is the hashed value of the object. The hashed values are generally used for the faster comparison between the two objects as the hash values are directly compared rather than comparing each object's value.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

How does hash() work on custom objects?

_hash_() method is called internally while we use the hash() function in python. So in order to override the _hash_() method we need to have our own custom definition of _eq_() and _hash_() functions. Below is the example where we can see how we can use the hash() function on custom objects.

Code:

Output:

More Examples

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 1: Using hash() with integer, string and float

Code:

Output:

Explanation:

As we said, applying the hash() function on integer, string, and float values will fetch the integer value as output, which are the hash values of the original inputs.

Example 2: Using hash() with immutable tuple objects

Code:

Output:

Explanation:

On applying the hash() function on an immutable object, we get the integer value which is the hash value of the entire set.

Example 3: Using hash() with the mutable object

Code:

Output:

Explanation: On applying the hash() function on mutable objects an error is raised saying unhashable type as the list is a mutable object which cannot be hashed using the hash function.

Conclusion

  • hash() function is used to fetch the hash values of the python objects.
  • hash() function returns the integer value if the hashable object passed in the function as a parameter.
  • hash() function calls inbuilt __hash__() function which can be overriden for hashing the custom objects.
  • hash() function can be applied to immutable objects.
  • hash() function cannot be applied to mutable objects.

See Also