Fix Python – Why does integer division yield a float instead of another integer?

Question

Asked By – Jonas Byström

Consider this division in Python:

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 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?

Now we will see solution for issue: Why does integer division yield a float instead of another integer?


Answer

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

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

This question is answered By – Brandon E Taylor

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0