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. Is there a maximum value for an int in Python?

    No. Python 3 integers have arbitrary precision: they grow to use as much memory as the value needs, so they never overflow. sys.maxsize is only the largest index a container can have, not the largest int. Floats, by contrast, are 64-bit and do overflow to inf.

    BeginnerQuick answer1 min read
  2. What is the difference between /, // and % in Python, especially with negative numbers?

    / is true division and always returns a float. // is floor division: it rounds down toward negative infinity. % returns the remainder that matches floor division, so the result takes the sign of the divisor. For example -7 // 2 is -4 and -7 % 2 is 1.

    BeginnerQuick answer1 min read
  3. What are Decimal and Fraction in Python, and when would you use them instead of float?

    Decimal does exact base-10 arithmetic, which suits money. Fraction stores a ratio of two integers and gives exact results for things like 1/3. Both avoid the binary rounding errors of float, at the cost of speed.

    IntermediateQuick answer1 min read