Integer Division's New Default: Behavior Shift

Python 3 changed integer division (`/`) to always return a float (e.g., `5 / 2` yields `2.5`), unlike Python 2 where it performed floor division for integers, often leading to subtle bugs if not explicitly handled with `//` for integer results.

The top 3

  1. Python's Division Shift: Why the Big Change?: Python 3 changed the `/` operator to always perform float division, unlike Python 2's ambiguous behavior, to achieve a unified numeric model and align with mathematical 'true division' expectations, as detailed in PEP 238.
  2. Migration Minefield: Division's Hidden Python 2-to-3 Bugs: The change in integer division was a major semantic hurdle during Python 2 to 3 migration, frequently causing subtle runtime bugs and incorrect calculations if developers didn't explicitly switch to `//` for floor division.
  3. Python's // vs. C++/Java's /: Division Differences: Unlike C++ or Java, where integer division with `/` truncates towards zero, Python 3's `//` operator performs 'floor division,' rounding results towards negative infinity, especially evident with negative numbers (e.g., -5 // 2 yields -3, not -2).

Sources

Open the full topic