Integers and Floating-Point Numbers
In Python 3, the / operator always returns a float, even when dividing two integers—a significant change from Python 2.
A Surprising Quotient
Consider the expression 5 / 2. Both operands are integers, but Python 3 does not produce an integer result. The / operator produces a floating-point result. This behavior is a significant change from Python 2 and is the starting point for choosing the correct division operator.
What do you think happens?
Before running 5 / 2 in Python 3, what value and result kind do you predict?
Reveal answer
Answer: 2.5, a floating-point result
In Python 3, / always returns a float, even when both operands are integers.
Reading the Division Operator
The operator determines the kind of division Python performs. In Python 3, a slash, /, always returns a float. Therefore, 8 / 4 produces a floating-point result even though the division has no remainder. The operands being integers does not change this rule.
A floating-point result is the result produced by Python 3's / operator. It remains a floating-point result even when the operands supplied to / are integers.
Choosing Floored Division
Use //, called floored division, when you need an integer result. Floored division rounds down toward negative infinity. For positive values such as 5 // 2, the result is 2. The word down is important: it does not mean simply removing the fractional part in every situation.
Selecting the Integer Result
You need the floored result of dividing 5 by 2. Which operator should you choose, and what result follows?
Identify the required result: The requirement is an integer result rather than the exact floating-point quotient.
Select the operator: Use // because Python defines it as floored division.
Evaluate the expression: The expression 5 // 2 gives 2 because 2 is the value reached by rounding the quotient down toward negative infinity.
Use 5 // 2 when the required result is the floored integer 2.
Following the Number Line
Floored division is especially important for negative numbers. The result moves toward negative infinity, not toward zero. Therefore, -5 // 2 equals -3, not -2. A result of -2 would reflect truncating the fractional part toward zero, but that is not the rule used by //.
What do you think happens?
What does -5 // 2 produce?
Reveal answer
Answer: -3, because floored division moves toward negative infinity
For negative values, flooring does not mean truncating toward zero. The next lower integer than -2.5 is -3.
Matching Operator to Intent
Choose the operator from the result your program needs. Choose / when you need Python 3's floating-point quotient. Choose // when you need floored division and an integer result. Treat this as a requirement-matching decision rather than as a question about whether the operands happen to be integers.
| Requirement | Operator | Result behavior |
|---|---|---|
| Floating-point quotient | / | Python 3 returns a float |
| Floored integer result | // | The result is rounded toward negative infinity |
A compact operator-selection guide
Mistakes Beginners Make
Assuming that two integer operands force an integer result.
In Python 3, / always returns a floating-point result.
Fix:
Use // when a floored integer result is required.Calling / truncating division.
Python 3's / operator returns a floating-point result rather than truncating the quotient to an integer.
Fix:
Use / for the floating-point quotient and // for floored division.Treating // as truncation toward zero.
Floored division rounds toward negative infinity.
Fix:
Expect -5 // 2 to equal -3.Choosing an operator based only on the operand types.
The required result, not just the operand type, determines the appropriate operator.
Fix:
Choose / for a floating-point quotient and // for a floored integer result.
Check Your Prediction
For each expression, predict the result before checking it: 9 / 2, 9 // 2, and -9 // 2. For each one, identify whether the result is a floating-point result or a floored integer result, and explain the direction of rounding where it applies.
Hints
- Start by identifying whether the operator is / or //.
- Remember that / always returns a float in Python 3.
- For the negative // expression, move toward negative infinity rather than toward zero.
Practice Answers
Determine the behavior of 9 / 2, 9 // 2, and -9 // 2.
Evaluate 9 / 2: The / operator returns a floating-point quotient, so the result is 4.5.
Evaluate 9 // 2: The // operator performs floored division. For this positive quotient, the floored result is 4.
Evaluate -9 // 2: The quotient lies between -4 and -5, and flooring moves toward negative infinity, so the result is -5.
The expressions produce 4.5, 4, and -5 respectively.
Essential Rules
- In Python 3, / always returns a floating-point result, even when both operands are integers.
- Use / when the exact floating-point quotient is the required kind of result.
- Use // when a floored integer result is required.
- Floored division rounds toward negative infinity, so -5 // 2 equals -3 rather than -2.
- Do not confuse floored division with truncation toward zero.
Key Takeaways
- Python 3's / operator always returns a floating-point result.
- The fact that operands are integers does not make / produce an integer.
- The // operator provides floored division and is used when an integer result is needed.
- Floored division moves toward negative infinity, not toward zero.
- Choose / or // according to the result your program requires.