What is the output of the following Python code? d = {1: “a”, 1.0: “b”}; print(d)
Correct answer D. {1: 'b'}
Explanation
Dictionary keys are compared by equality and hash, and 1 == 1.0 with equal hashes, so both entries use the same key. The first key object, the int 1, is kept, while the value is overwritten by the later "b". The result is {1: 'b'}, not two separate entries.
Answer checked How we write and check questions
Discussion