Python hash Function with Examples - Python Tutorial | Logic Practice
Python Course / Other / Python hash Function with Examples

Python hash Function with Examples

BLUF: This lesson on Python hash Function with Examples provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python hash Function with Examples

Mastering Python hash Function with Examples is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

The hash function in Python is utilized to obtain the hash value of a given object. Python determines this hash value by employing a hashing algorithm. These hash values are represented as integers and are essential for comparing keys within a dictionary when performing a lookup. The following types can be hashed:

Types that are hashable include:

  • boolean (bool)
  • integer (int)
  • long integer (long)
  • floating-point number (float)
  • string
  • Unicode
  • tuple
  • code object

We cannot hash of these types:

Types that cannot be hashed include:

  • bytearray
  • list
  • set
  • dictionary
  • memoryview
  • Python hash Function Syntax

It has the following syntax:

Example

hash (object)

Parameters

  • object: The item for which we seek to obtain a hash. Only types that are immutable are eligible for hashing.
  • Return

It returns the hash value of an object.

Difference between Python hash Function

Let’s examine a few examples of the hash function to gain a better understanding of its operations.

Python hash Function Example 1

In this section, we will obtain the hash values for both integer and floating-point numbers. Please refer to the example provided below.

Example

# Python hash() function example

# Calling function

result = hash(21) # integer value

result2 = hash(22.2) # decimal value

# Displaying result

print(result)

print(result2)

Output:

Output

21

461168601842737174

Python hash Function Example 2

This function can be utilized on the iterable elements to generate hash values.

Example

# Python hash() function example

# Calling function

result = hash("logicpractice") # string value

result2 = hash((1,2,22)) # tuple value

# Displaying result

print(result)

print(result2)

Output:

Output

-3147983207067150749

2528502973955190484

Python hash Function Example 3

Consider the following illustration regarding the hash function in Python.

Example

# Python hash() function example

# Calling function

result = hash("logicpractice") # string value

result2 = hash([1,2,22]) # list

# Displaying result

print(result)

print(result2)

Output:

Output

TypeError: unhashable type: 'list'

Python hash Function Example 4

In this instance, we are providing a newly created custom object to the function. The output of the function is the hash corresponding to this object.

Example

# Python hash() function example

class Student:

    def __init__(self,name,email):

        self.name = name

        self.email = email

student = Student("Arun", "[email protected]")

# Calling function

result = hash(student) # object

# Displaying result

print(result)

Output:

Output

8793491452501

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience