Why does integer division yield a float instead of another integer?

ghz 1years ago ⋅ 3314 views

Question

Consider this division in Python 3:

>>> 2/2
1.0

Is this intended? I strongly remember earlier versions returning int/int = int. What should I do? Is there a new division operator or must I always cast?


In 2.x, the behaviour was indeed reversed; see How can I force division to be floating point? Division keeps rounding down to 0? for the opposite, 2.x-specific problem.


Answer

Take a look at PEP-238: Changing the Division Operator

The // operator will be available to request floor division unambiguously.