/
True division: 7 / 2 is 3.5, and even 8 / 2 is 4.0 — floats, always.
Common call
7 / 2
Returns
float, even for evenly-dividing ints
Replaces
integer division is // — a different operator
Watch out
division by zero raises ZeroDivisionError
aa — Dividend.type: number · required / bb — Divisor — zero raises.type: number · required
→ float
Demo
Live evaluation
Try:
Inputs
afloatdividend
bfloatdivisor
Output
7 / 2
3.5
The result is a true quotient — 7 / 2 gives 3.5, no truncation. (Python would print the evenly-divided case as 4.0 to mark it as a float; the demo shows the numeric value.) Dividing by zero raises Python’s exact error.
Operands
| Name | Type | Required | Description |
|---|---|---|---|
| a | number | yes | Dividend. |
| b | number | yes | Divisor — zero raises. |
Return value
float — The quotient as a float — ALWAYS, even when both operands are ints and divide evenly.
Common patterns
Averages
The float result is what you want for means.
mean = sum(xs) / len(xs)
Guarded division
Handle the zero case explicitly when the divisor is data.
rate = hits / total if total else 0.0
Examples
1. True quotient
7 / 2
Returns
3.52. Even division is still float
8 / 2
Returns
4.03. Ints promote
1 / 3
Returns
0.3333333333333333Pitfalls
1. Expecting an int result
Python 3 / never truncates — that is //.
Float where int expected
index = total / 2 items[index]
TypeError: list indices must be integers
Fix
index = total // 2
int index
2. Division by zero raises
Not infinity, not NaN — an exception.
Raises
1 / 0
ZeroDivisionError: division by zero
Guard
x / y if y else float("inf")
explicit choice
When to use
Use it
- Any division where the fractional part matters
- Averages, rates, ratios
Reach for something else
- Whole-number division → //
- Quotient AND remainder → divmod()
- Exact decimal division → decimal.Decimal
Notes
Complexity
O(1)
Return
float (int/int included)
CPython impl
Objects/abstract.c :: PyNumber_TrueDivide → __truediv__
Memory
No allocation beyond the result
Thread-safe
Yes — pure computation
FAQ
Python 3 made / always produce a float so the result type never depends on the values. Truncating division is a separate operator, //.
History
3.0
int / int now returns float — the true-division switch (PEP 238).