Interview Q&A / Python

INTERVIEW Q&A · 81 QUESTIONS

Python interview questions

Lists, dictionaries, generators, decorators and the Python questions asked in service-company and data roles.

  1. Why does 0.1 + 0.2 == 0.3 return False in Python, and how do you compare floats correctly?

    Floats are stored in binary, and 0.1 and 0.2 have no exact binary form, so their sum is 0.30000000000000004. Compare floats with math.isclose(), or use decimal.Decimal when exact decimal arithmetic is needed, such as for money.

    BeginnerQuick answer1 min read