Python Glossary Русский

Python 3.0+Arithmetic & operatorsterm

/ division

arithmetic

The division operator. For int and float it always returns a float, even when both operands are integers. Division by zero raises ZeroDivisionError.

Syntax

a / b -> float

For int and float the result is always a float (complex, Decimal and Fraction keep their own type), and a float has only 53 bits of mantissa: with large integers the low digits are silently lost, and truly huge values such as 10**400 / 2 raise OverflowError — when you need an exact integer answer use // instead of dividing and converting back. Dividing by 0.0 also raises ZeroDivisionError rather than producing inf as C or NumPy would.

Examples

print(10 / 2)  # → 5.0 (всегда float)
print(9 / 3)  # → 3.0
print(7 / 2)  # → 3.5
print(1 / 3)  # → 0.3333333333333333
print(type(6 / 2))  # → <class 'float'>
try:
    print(1 / 0)
except ZeroDivisionError:
    print("Деление на ноль!")  # → Деление на ноль!

See also

Open in the full glossarydocs.python.orgReport an inaccuracy