> The classic example is division: in Python 2, 1 / 2 is 0; while in Python 3 it is 0.5.
Can someone kindly explain this?
edit: I misread it as “In Python 2 2.1/2 is 0, while in Python 3 it is 0.5”
> The classic example is division: in Python 2, 1 / 2 is 0; while in Python 3 it is 0.5.
Can someone kindly explain this?
edit: I misread it as “In Python 2 2.1/2 is 0, while in Python 3 it is 0.5”
From long ago memory: in Python 2, 1 / 2 did integer division. You had to do something like 1 / 2.0 if you wanted floating point division.
Spot on. Py2 was integer division, Py3 was true division. In Py2 you could get the true division with an `from __future__ import division` (Can't recall which Py2 version this became an import). Fun fact too: In Py3 you can do a // to get back to the floor division effect (e.g. 1 // 2 --> 0)
PEP 238 lays out the story if anyone is curious. Well into the Python 3 transition, I still had a habit of multiplying the denominator by 1.0 to ensure it was a float.
https://peps.python.org/pep-0238/