Today we’ll be talking about the difference between true division and floor division in Python.
There are two kinds of division operators:
1) true division /
2) floor division //
In true division the result of dividing two integers is a float:
>>> 12 / 4
3.0
>>> 20 / 3
6.666666666666667
In floor division the result is truncated down, so the result is the floor – the largest integer number smaller than the result of true division:
>>> 12 // 4
3
>>> 20 // 3
6
If either the dividend or the divisor or both are floats, the result is a float. If both are integers, the result is an integer:
>>> 10 // 5 # int // int -> int
2
>>> 10 // 3 # int // int -> int
3
>>> 10.0 // 3 # float // int -> float
3.0
>>> 10 // 3.0 # int // float -> float
3.0
>>> 10.0 // 3.0 # float // float -> float
3.0
>>> 5.6 // 2 # float // int -> float
2.0
Here’s the video version: