Concepts / The Modulo Operator: Finding Remainders

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.

  • Programming

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?

  • 2, an integer

  • 2.5, a floating-point value

  • 3, an integer

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.

5 / 2integer operands2.5floating-point result8 / 4integer operands2.0floating-point resultevaluates toevaluates to
What value and type does Python return when / divides pairs of integers?
Key point

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.

5 / 2ordinary division2.5float5 // 2floored division2integer resultreturnsreturns
How does the operator change the result when the same numbers are divided?
OperatorPurposeResult for 5 and 2
/Ordinary division2.5, a floating-point result
//Floored division2, an integer result

The operator determines whether Python performs ordinary division or floored division.

Negative Operands

Worked example

Why -5 // 2 Is -3

Determine the result of -5 // 2.

  1. 1
    Identify the operator

    The expression uses //, so Python applies floored division.

  2. 2
    Apply the direction of rounding

    Floored division rounds down toward negative infinity, not toward zero.

  3. 3
    Determine the result

    Rounding down in this case gives -3 rather than -2.

Result

-5 // 2 equals -3.

-5 / 2ordinary division-2.5floating-point result-5 // 2floored division-3rounded toward negativeinfinityreturnsreturns
What happens to the fractional part when / is replaced by // for a negative division?

Predict Before Evaluating

MEDIUM

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.
Worked example

Checking the Predictions

Determine the results of 7 / 3, 7 // 3, and -5 // 2.

  1. 1
    Evaluate 7 / 3

    Because / always returns a float in Python 3, the result is 2.3333333333333335, a floating-point result.

  2. 2
    Evaluate 7 // 3

    Because // performs floored division and the result is positive, the result is 2, an integer result.

  3. 3
    Evaluate -5 // 2

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

Result

7 / 3 produces 2.3333333333333335, 7 // 3 produces 2, and -5 // 2 produces -3.

Frequent Operator Mistakes

  • Common mistake

    Assuming that two integer operands make / return an integer.

    What it looks like

    5 / 2 is expected to be 2.

    Why it fails

    In Python 3, / always returns a floating-point result.

    Do this instead

    Expect 5 / 2 to produce 2.5. Use // when a floored integer result is needed.

  • Common mistake

    Treating // as truncation toward zero.

    What it looks like
    -5 // 2 is expected to be -2.
    Why it fails

    Floored division rounds down toward negative infinity.

    Do this instead
    Remember that -5 // 2 equals -3.
  • Common mistake

    Choosing an operator without considering the required result.

    What it looks like

    Using / when the task requires an integer result.

    Why it fails

    The / operator returns a float in Python 3.

    Do this instead

    Choose // when floored division and an integer result are needed; choose / when ordinary floating-point division is wanted.

Operator Checklist

  1. 1
    In Python 3, / always returns a floating-point result, even when both operands are integers.
  2. 2
    Use // for floored division when an integer result is needed.
  3. 3
    Floored division rounds toward negative infinity, not toward zero.
  4. 4
    The expression -5 // 2 equals -3.
  5. 5
    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.

More concepts starting with T