Python Glossary Русский

all Python 3operatorfunction

operator.truediv

arithmetic

Return a / b — true division (the functional form of `/`).

Syntax

operator.truediv(a, b)

The result for two ints is always a float, even when the division is exact: truediv(6, 3) returns 2.0, not 2. For an integer result use floordiv or divmod; for exact arithmetic use fractions.Fraction. Division by zero raises ZeroDivisionError for both int and float — you will not get an infinity as in NumPy.

Examples

import operator
print(operator.truediv(7, 2))   # → 3.5
print(operator.truediv(7, 2) == 7 / 2)   # → True
print(operator.truediv(6, 3))   # → 2.0
print(list(map(operator.truediv, [10, 9], [4, 3])))   # → [2.5, 3.0]
print(operator.truediv(1, 0))   # → ZeroDivisionError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy