Integer division

What does double slash do in python, and what is integer division?

You are viewing a single comment. View All
olafh10 (31)

In programming, there are two types of divisions; integer division and float division. Integer stands for a solid number (like 1, 2, 3, 4 etc), while a float is a number with a decimal part (1.0, 1.2, 1.3, 1.438345 etc)

Python 2 tried to keep an integer an integer, and a float a float. Thus, you could only get a float result by having a float in your division:
(Python 2):

``````>>> 10.0/3
3.3333333333333335
>>> 10/3
3``````

See? In the second calculation the result is rounded to a whole number in order that it counts as an integer.

Python 3 changed this. In order to make it (logically) easier, they stopped trying to give you a integer back if you input an integer:
(Python 3):

``````>>> 10/3
3.3333333333333335``````

But, sometimes you WANT a number to be rounded for some reason. To give you the possibility to still do that, Python introduced the `//` operator in Python 3. This operator does what Python 2 would do when trying to divide with integers:
(Python 3):

``````>>> 10//3
3``````

If you want a pretty technical explanation along with arguments, check out PEP0238 (The archive case for this feature)