Division
suggest changePython 3 has separate operators for true division and floor division.
True division
The / operator always performs true division and returns a floating-point result, even when both operands are integers:
3 / 2 # 1.5
-3 / 2 # -1.5
10 / 2 # 5.0
The equivalent function in the operator module is operator.truediv:
import operator
operator.truediv(3, 2) # 1.5
Floor division
The // operator rounds the mathematical quotient down toward negative infinity. If either operand is a float, the result is also a float:
3 // 2 # 1
-3 // 2 # -2
3 // 2.0 # 1.0
The equivalent function is operator.floordiv:
operator.floordiv(3, 2) # 1
Do not use a from __future__ import division statement or the removed operator.div function; those belonged to Python 2 compatibility code.
See PEP 238 for the rationale behind the two operators.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents