Python floor is done with //
, the floor division operator.
>>> 5//2
2
>>> -5//2
-3
It divides two numbers and rounds down the result to the nearest integer. Don’t confuse it with integer division, since it doesn’t just round to the nearest integer, for example:
>>> -10 / 3
-3.3333333333333335
>>> -10 // 3
-4
>>> 20 / 3
6.666666666666667
>>> 20 // 3
6
You can also use floats for floor division:
>>> 5.5 // 3
1.0
If any of the numbers in the operation are floats, Python will return a float.
Doing floor division with zero as the divisor will throw a ZeroDivisionError
>>> 3 // 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero