Type Conversion and Casting in Python
In Python 3, the / operator always returns a float, even when dividing two integers—a significant change from Python 2.
The Division Surprise
A common beginner expectation is that dividing two integers produces another integer. In Python 3, that expectation is wrong for the / operator. Python 3 always produces a floating-point result with /, even when both operands are integers. This differs from the division behavior associated with Python 2.
What do you think happens?
What result should Python 3 produce for 5 / 2?
Reveal answer
Answer: 2.5
The / operator returns a floating-point result in Python 3, so dividing 5 by 2 produces 2.5 rather than an integer.
Tracing the Result Type
When Python evaluates an expression using /, the important change is not only the numerical value. The result is also a floating-point value. Thus, the expression 8 / 4 produces 2.0 rather than 2. The operands are integers, but the result of / is a float.
For Python 3, remember this rule: / produces a float, even when both values being divided are integers.
Choosing Between / and //
| Operator | Result behavior | Generated example |
|---|---|---|
| / | Returns a floating-point result | 5 / 2 produces 2.5 |
| // | Uses floored division to obtain an integer result | 5 // 2 produces 2 |
Use / when the calculation needs the floating-point quotient. Use // when the calculation needs the floored result. The word floored matters: // rounds down toward negative infinity, not toward zero.
Selecting the Operator
A calculation divides 5 by 2. Determine the result when the calculation needs the precise quotient and when it needs a floored integer result.
Precise quotient: Use / because Python 3 returns the floating-point quotient. The result is 2.5.
Floored result: Use // because floored division is used to obtain an integer result. The result is 2.
Operator choice: The correct operator depends on whether the calculation needs the floating-point result or the floored result.
Use / for 2.5 and // for the floored result 2.
Why Truncation Is the Wrong Model
It is inaccurate to describe Python 3 division with / as truncating the quotient to an integer. For example, 5 / 2 produces 2.5, not 2. If an integer result is needed, // is the relevant operator, but its behavior is flooring rather than truncation toward zero.
Assuming that two integer operands make / return an integer.
In Python 3, / always returns a floating-point result.
Fix:
Expect 5 / 2 to produce 2.5.Using / when a floored integer result is required.
The / operator returns 2.5 rather than a floored result.
Fix:
Use // when a floored result is required.Treating // as truncation toward zero.
Floored division rounds down toward negative infinity.
Fix:
Expect -5 // 2 to produce -3.
Operator Selection Practice
For each expression, predict the result and identify whether the expression uses floating-point division or floored division: 9 / 2, 9 // 2, and -5 // 2.
Hints
- Remember that / always returns a floating-point result in Python 3.
- Remember that // floors toward negative infinity.
- Check the operator before calculating the quotient.
Practice Check
Determine the results of 9 / 2, 9 // 2, and -5 // 2.
9 / 2: The / operator returns a floating-point result, so the result is 4.5.
9 // 2: The // operator gives the floored result, so the result is 4.
-5 // 2: Flooring moves toward negative infinity, so the result is -3 rather than -2.
The results are 4.5, 4, and -3, respectively.
Key Takeaways
- In Python 3, / always returns a floating-point result, even when both operands are integers.
- Use / when the calculation needs the floating-point quotient.
- Use // when the calculation needs a floored result.
- Floored division rounds toward negative infinity, so -5 // 2 equals -3 rather than -2.
- Python 3 division with / does not truncate integer operands to an integer result.