Python Reference Library →

Python hash() Function – Generate Hash Values of Objects

Let’s look at hash() function – generate hash values of objects

  1. Notice

    hash() returns an integer hash for a hashable object. Hash equality does not prove object equality, and string hashes may vary across processes.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 3
print(hash(number))
Live state

Top-level scope

number→Object 1
Object 1 · int3
This step

hash() returns an integer hash for a hashable object. Hash equality does not prove object equality, and string hashes may vary across processes.

Picture hash() function – generate hash values of objects

hash() returns an integer hash for a hashable object. Hash equality does not prove object equality, and string hashes may vary across processes.

Image brief

Alt text
A diagram for Python hash() Function – Generate Hash Values of Objects. Instructions: number = 3 print(hash(number)). Displayed result: 3. The final example bindings are number --> object 1 (int). object 1: 3
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python hash() Function – Generate Hash Values of Objects". Teaching focus: hash() returns an integer hash for a hashable object. Hash equality does not prove object equality, and string hashes may vary across processes. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 3 print(hash(number)) | v DISPLAYED RESULT 3 FINAL BINDINGS number --> object 1 (int) object 1: 3 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python hash() Function – Generate Hash Values of Objects. Instructions: number = 3 print(hash(number)). Displayed result: 3. The final example bindings are number --> object 1 (int). object 1: 3

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 3
print(hash(number))
Live state

Top-level scope

number→Object 1
Object 1 · int3
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
3

What do you expect this program to print?

What does this example display?

number = 3
print(hash(number))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3

Follow the printed values in order. The output is: 3

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

hash() returns an integer hash for a hashable object. Hash equality does not prove object equality, and string hashes may vary across processes.

hash() returns an integer hash for a hashable object. Hash equality does not prove object equality, and string hashes may vary across processes.

More examples and reference

Python hash() Function

The hash() function in Python returns the hash value of an object. It is commonly used in hashing data structures like sets and dictionaries.

Syntax

hash(object)

Parameters:

  • object – The input object must be immutable (like int, float, str, tuple with only hashable items).

Returns:

  • An integer hash value that represents the object.

Example 1: Hashing Numbers

print(hash(42))
print(hash(-42))
print(hash(3.14))
(Output will be integer values like 42 or -42 or some number)

Example 2: Hashing Strings

print(hash("hello"))
print(hash("HELLO"))
(Will return integer hash values for each string)

Example 3: Using hash() with Tuples

print(hash((1, 2, 3)))
(Returns an integer hash)

Use Case: Why use hash()?

  • To store objects in sets or as dictionary keys.
  • To compare objects efficiently by their hash.
  • To build data structures like hash tables.

Important Notes

  • Only immutable objects can be hashed.
  • Mutable types like list or dict will raise a TypeError.
  • Hash values may differ across Python sessions (especially for strings).

Example 4: Unhashable Object (Throws Error)

print(hash([1, 2, 3]))  # List is mutable
TypeError: unhashable type: 'list'

Custom Hash in Classes

class MyClass:
    def __init__(self, value):
        self.value = value

    def __hash__(self):
        return hash(self.value)

obj = MyClass(100)
print(hash(obj))
(Depends on the value passed)

Interview Tip

Interviewers often ask about hash-based data structures. Understanding hash() is key to explaining how dict and set work in Python.

Summary

  • hash() returns a unique integer based on the object's content.
  • Used in sets, dicts, and custom objects.
  • Only immutable objects can be hashed.

Practice Problem

Write a program to find and print the hash values of a string, a tuple, and an integer input from the user.

text = input("Enter a string: ")
number = int(input("Enter an integer: "))
tpl = (text, number)

print("Hash of string:", hash(text))
print("Hash of integer:", hash(number))
print("Hash of tuple:", hash(tpl))

What to remember

Connect what you noticed in the example with the idea below.

Remember

hash() returns an integer hash for a hashable object. Hash equality does not prove object equality, and string hashes may vary across processes.

Watch out for this idea

Equal hashes guarantee that two objects are equal.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →