Practice mode Python

What is the output of the following Python code? print(0.1 + 0.2 == 0.3)

Correct answer B. False

Explanation

Floats are stored in binary, and 0.1, 0.2 and 0.3 cannot be represented exactly, so 0.1 + 0.2 produces 0.30000000000000004, which is not equal to the stored 0.3. Use math.isclose() or the decimal module when comparing or handling decimal fractions.

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.