Practice mode Python

What is the output of the following Python code? print(len({1, True, 1.0}))

Correct answer A. 1

Explanation

A set keeps only one of any group of equal elements. In Python True == 1 and 1 == 1.0, and these values also have equal hashes, so the set treats all three as the same element and keeps only the first one inserted. The length is therefore 1, not 3.

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.