Operators and Operands: Performing Computations
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 assumption is that dividing two integers should produce another integer. In Python 3, that assumption is wrong for the / operator. Python 3 always returns a floating-point result from /, even when both operands are integers. This behavior is a significant difference from Python 2.
What do you think happens?
What value and type will Python 3 produce for 7 / 2?
Reveal answer
Answer: 3.5, a floating-point value
The / operator always returns a float in Python 3. Dividing 7 by 2 gives the mathematical result 3.5, so Python produces 3.5 rather than truncating the result to 3.
Tracing True Division
Think of / as Python 3's operator for obtaining the division result in floating-point form. The operands 7 and 2 are integers, but the result is 3.5, a floating-point value. The fact that the inputs are integers does not make the output an integer.
Evaluating 9 / 4
Predict the value and kind of result produced by 9 / 4 in Python 3.
Identify the operator: The expression uses /, so Python 3 applies true division.
Divide the operands: Nine divided by four is 2.25.
Determine the result form: Because / always returns a float in Python 3, the result is a floating-point value rather than an integer.
9 / 4 produces 2.25, a floating-point result.
Choosing Floored Division
Use // when you need an integer result from division. This operator is called floored division because it rounds the result down toward negative infinity. For positive values, this often looks like dropping the fractional part, but that description is not sufficient for negative values.
Evaluating 9 // 4
Predict the result when the same operands are evaluated with floored division.
Identify the operator: The expression uses //, which is floored division.
Find the division result: Nine divided by four is 2.25.
Apply flooring: Floored division moves down to the integer 2.
9 // 4 produces 2, an integer result.
Negative Operands
The word floored is important. Floored division does not simply truncate toward zero. For -5 // 2, the mathematical division result is -2.5. Rounding down toward negative infinity gives -3, so -5 // 2 equals -3, not -2.
Mistakes in Prediction
Assuming that two integer operands force / to return an integer.
In Python 3, / always returns a floating-point result, even when both operands are integers.
Fix:
Expect 7 / 2 to produce 3.5. Use // when a floored integer result is required.Treating / and // as interchangeable.
The two operators produce different kinds of results: / returns a float, while // performs floored division.
Fix:
Select the operator based on the required result: / for floating-point division and // for floored division.Describing // as truncation toward zero.
Floored division rounds down toward negative infinity, so the result is -3.
Fix:
For negative results, apply the rule toward negative infinity: -5 // 2 equals -3.
| Expression | Operator meaning | Result |
|---|---|---|
| 7 / 2 | True division | 3.5, a floating-point result |
| 9 / 4 | True division | 2.25, a floating-point result |
| 9 // 4 | Floored division | 2, an integer result |
| -5 // 2 | Floored division toward negative infinity | -3, not -2 |
Practice and Review
For each expression, predict the value and whether the result is floating-point or integer: 8 / 3, 8 // 3, and -8 // 3. Then explain why the last result is not obtained by truncating toward zero.
Hints
- The / operator always produces a floating-point result in Python 3.
- The // operator rounds down toward negative infinity.
- For the negative expression, compare rounding down with moving toward zero.
- Python 3's / operator always returns a floating-point result, even when both operands are integers. Use // when you need floored division and an integer result. For positive operands, // often appears to remove the fractional part, but its real rule is rounding down toward negative infinity. Therefore, -5 // 2 equals -3, not -2. The safest way to predict a division expression is to inspect the operator first and then apply its specific rule.
Key Takeaways
- In Python 3, / always produces a floating-point result.
- The integer operands in an expression do not make / return an integer.
- Use // for floored division when an integer result is required.
- Floored division rounds toward negative infinity, so -5 // 2 equals -3 rather than -2.