03 / 12

What is the difference between `/` and `//` in Python?

Difficulty: 2/10
Operators & Expressions, Division Operators, Floor Division and Modulo Semantics

True division vs floor division

/ is true division: in Python 3 it always returns a float for int and float operands (PEP 238 changed this from Python 2). // is floor division: it divides and rounds toward negative infinity, not toward zero. The result type follows the operands: int // int is an int, anything involving a float gives a float (3.0, not 3).

javascript

Why floor, not truncate: floor division is paired with the modulo operator so that a == (a // b) * b + a % b always holds and a % b takes the sign of b. That makes things like wrap-around indexing ((i - 1) % n) behave sensibly for negatives, whereas C and Java truncate and produce negative remainders. This is the number one gotcha for engineers coming from those languages: -7 // 2 is -4 in Python.

Trade-offs and alternatives: use // when you need whole-number results, for example pagination or bucket indices, and use divmod when you need quotient and remainder together. For exact fractions use fractions.Fraction; for money use decimal.Decimal or integer cents. For large ints, / converts to float and can lose precision or raise OverflowError, so // (or Fraction) is the safe choice.

Common mistakes: using int(a / b) for integer division (it goes through float and can be wrong for large values and gives a different rounding direction for negatives), assuming // always returns an int, and trusting float // results (see 1 // 0.1). When porting Python 2 code, every bare / on ints silently changes meaning, which is a classic migration bug.

Scenario Questions

0-2 years experience

  1. 1Compute 17 / 5, 17 // 5, -17 // 5 and -17 % 5 by hand and explain the results.
  2. 2You have 103 items and want 10 per page. Which operator gives you the number of pages needed, and what is the idiom for ceiling division?

2-5 years experience

  1. 1A billing routine uses int(a / b) and is occasionally off by one for very large integers. Why does it happen, and what is the fix?
  2. 2Write a function converting seconds to hours, minutes and seconds using divmod. How does it behave for negative input, and how would you handle that?

5-8 years experience

  1. 1You are porting a Python 2 codebase in which / is used on integers everywhere. How would you find and safely handle every occurrence?
  2. 2Why does -1 // 2 equal -1, and how does floor-based division and modulo make circular-buffer index arithmetic simpler than in C?

8+ years experience

  1. 1You are designing a numeric library type (say Money) that must interoperate with int, float, Decimal and Fraction. How do __truediv__, __floordiv__, __rtruediv__ and NotImplemented dispatch work, and how would you implement them?
  2. 2Discuss the pitfalls of // with floats (for example 1 // 0.1) and how you would guard against them in financial or scientific code.

Follow-up Questions

  • Why does -7 // 2 return -4 rather than -3?
  • How would you compute the ceiling of a division using only integer operations?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.