How do I hash tuples in Python with examples?

In Python, tuples are immutable sequences, and they can be hashed. This makes them suitable for use as keys in dictionaries or elements in sets. Python provides a built-in `hash()` function that computes the hash value of an object.

When hashing a tuple, each element in the tuple is hashed, and the overall hash value of the tuple is computed from these individual hash values. Here are some examples to illustrate this:

# Example of hashing tuples in Python tuple1 = (1, 2, 3) # This is a simple tuple tuple2 = ('apple', 'banana', 'cherry') # Tuple with strings tuple3 = (1, 2, (3, 4)) # Tuple with nested tuple print("Hash of tuple1:", hash(tuple1)) print("Hash of tuple2:", hash(tuple2)) print("Hash of tuple3:", hash(tuple3))

tuples hash python immutable hash function