Concepts / Operators and Operands: Performing Computations

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.

  • Programming

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?

  • 3, an integer

  • 3.5, a floating-point value

  • 4, an integer

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

7integer operand2integer operand/true division3.5floating-point resultleft operandright operandproduces
What value and type does Python 3 produce when two integer operands are evaluated with /?

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.

Worked example

Evaluating 9 / 4

Predict the value and kind of result produced by 9 / 4 in Python 3.

  1. 1
    Identify the operator

    The expression uses /, so Python 3 applies true division.

  2. 2
    Divide the operands

    Nine divided by four is 2.25.

  3. 3
    Determine the result form

    Because / always returns a float in Python 3, the result is a floating-point value rather than an integer.

Result

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.

9integer operand4integer operand/true division//floored division2.25floating-point result2integer resultwithwithproduceswithwithproduces
How do the two division operators transform the same operands, and which one gives an integer result?
Worked example

Evaluating 9 // 4

Predict the result when the same operands are evaluated with floored division.

  1. 1
    Identify the operator

    The expression uses //, which is floored division.

  2. 2
    Find the division result

    Nine divided by four is 2.25.

  3. 3
    Apply flooring

    Floored division moves down to the integer 2.

Result

9 // 4 produces 2, an integer result.

Negative Operands

-5 / 2division result-5 // 2floored division-2.5floating-point result-3integer resultproducesproduces
What changes when -5 is divided by 2 using floored division rather than stopping at the integer closer to zero?

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

  • Common mistake

    Assuming that two integer operands force / to return an integer.

    What it looks like

    7 / 2 is expected to produce 3.

    Why it fails

    In Python 3, / always returns a floating-point result, even when both operands are integers.

    Do this instead

    Expect 7 / 2 to produce 3.5. Use // when a floored integer result is required.

  • Common mistake

    Treating / and // as interchangeable.

    What it looks like

    Using / when the required result is an integer.

    Why it fails

    The two operators produce different kinds of results: / returns a float, while // performs floored division.

    Do this instead

    Select the operator based on the required result: / for floating-point division and // for floored division.

  • Common mistake

    Describing // as truncation toward zero.

    What it looks like
    Expecting -5 // 2 to produce -2.
    Why it fails

    Floored division rounds down toward negative infinity, so the result is -3.

    Do this instead

    For negative results, apply the rule toward negative infinity: -5 // 2 equals -3.

ExpressionOperator meaningResult
7 / 2True division3.5, a floating-point result
9 / 4True division2.25, a floating-point result
9 // 4Floored division2, an integer result
-5 // 2Floored division toward negative infinity-3, not -2

Use the operator and the sign of the result to predict both the value and the result form.

Practice and Review

MEDIUM

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.
  1. 1
    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.

Related concepts

More concepts starting with O