Practice mode Python

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.