The Modulo Operator: Finding Remainders
In Python 3, the / operator always returns a float, even when dividing two integers—a significant change from Python 2.
A Division Surprise
A common assumption is that dividing two integers produces another integer. Python 3 does not work that way with the / operator. In Python 3, / always returns a floating-point result, even when both inputs are integers. This differs from Python 2 and is important whenever you need to predict an expression's result or choose an operator for a task.
What do you think happens?
What value and type does Python 3 produce for 5 / 2?
Reveal answer
Answer: 2.5, a floating-point value
Python 3's / operator always returns a float, including when both operands are integers.
The / Result
The / operator performs ordinary division and returns a floating-point result in Python 3. For example, 5 / 2 produces 2.5 rather than an integer. The key point is that the result type is not determined only by the types of the inputs: two integer operands still produce a floating-point result when / is used.
Do not infer that an integer result will be returned merely because both operands are integers. With / in Python 3, the result is a float.
The // Alternative
Use //, called floored division, when you need an integer result. Floored division does not simply remove the fractional part by moving toward zero. It rounds down toward negative infinity. This distinction matters especially when the numbers are negative.
| Operator | Purpose | Result for 5 and 2 |
|---|---|---|
| / | Ordinary division | 2.5, a floating-point result |
| // | Floored division | 2, an integer result |
The operator determines whether Python performs ordinary division or floored division.
Negative Operands
Why -5 // 2 Is -3
Determine the result of -5 // 2.
Identify the operator: The expression uses //, so Python applies floored division.
Apply the direction of rounding: Floored division rounds down toward negative infinity, not toward zero.
Determine the result: Rounding down in this case gives -3 rather than -2.
-5 // 2 equals -3.
Predict Before Evaluating
For each expression, predict the result and whether it is a floating-point result or an integer result: 7 / 3, 7 // 3, and -5 // 2.
Hints
- The / operator always returns a float in Python 3.
- The // operator performs floored division.
- For a negative result with //, round toward negative infinity rather than toward zero.
Checking the Predictions
Determine the results of 7 / 3, 7 // 3, and -5 // 2.
Evaluate 7 / 3: Because / always returns a float in Python 3, the result is 2.3333333333333335, a floating-point result.
Evaluate 7 // 3: Because // performs floored division and the result is positive, the result is 2, an integer result.
Evaluate -5 // 2: Floored division rounds toward negative infinity, so the result is -3.
7 / 3 produces 2.3333333333333335, 7 // 3 produces 2, and -5 // 2 produces -3.
Frequent Operator Mistakes
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. Use // when a floored integer result is needed.Treating // as truncation toward zero.
Floored division rounds down toward negative infinity.
Fix:
Remember that -5 // 2 equals -3.Choosing an operator without considering the required result.
The / operator returns a float in Python 3.
Fix:
Choose // when floored division and an integer result are needed; choose / when ordinary floating-point division is wanted.
Operator Checklist
- In Python 3, / always returns a floating-point result, even when both operands are integers.
- Use // for floored division when an integer result is needed.
- Floored division rounds toward negative infinity, not toward zero.
- The expression -5 // 2 equals -3.
- To predict a division expression, inspect the operator first and then account for the sign of the operands.
Key Takeaways
- Python 3's / operator always produces a floating-point result.
- The // operator performs floored division and is used when an integer result is needed.
- Floored division rounds toward negative infinity, so -5 // 2 equals -3 rather than -2.
- The belief that Python 3 truncates integer division is incorrect; the operator determines the result behavior.