Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
number = 3
print(hash(number))Top-level scope
3hash() returns an integer hash for a hashable object. Hash equality does not prove object equality, and string hashes may vary across processes.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
number = 3
print(hash(number))
|
v
DISPLAYED RESULT
3
FINAL BINDINGS
number --> object 1 (int)
object 1: 3Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
number = 3
print(hash(number))Top-level scope
3The example has finished. Compare the final output with the values and references above.
3
What do you expect this program to print?
number = 3
print(hash(number))hash() FunctionThe hash() function in Python returns the hash value of an object. It is commonly used in hashing data structures like sets and dictionaries.
hash(object)
object – The input object must be immutable (like int, float, str, tuple with only hashable items).print(hash(42))
print(hash(-42))
print(hash(3.14))
(Output will be integer values like 42 or -42 or some number)
print(hash("hello"))
print(hash("HELLO"))
(Will return integer hash values for each string)
print(hash((1, 2, 3)))
(Returns an integer hash)
list or dict will raise a TypeError.print(hash([1, 2, 3])) # List is mutable
TypeError: unhashable type: 'list'
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)
Interviewers often ask about hash-based data structures. Understanding hash() is key to explaining how dict and set work in Python.
hash() returns a unique integer based on the object's content.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))
Connect what you noticed in the example with the idea below.
hash() returns an integer hash for a hashable object. Hash equality does not prove object equality, and string hashes may vary across processes.
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.