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

Viewed 211801

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?

5 Answers

According to Python 3 documentation, Python when divided by integer, will generate float despite expected to be integer.

For exclusively printing integer,use floor division method. Floor division is rounding off zero and removing decimal point. Represented by //

Hence, instead of 2/2 ,use 2//2

You can also import division from __future__ irrespective of using Python 2 or Python 3.

Related