Concepts / Integers and Floating-Point Numbers

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.

  • Programming

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?

  • 2, an integer

  • 2.5, a floating-point result

  • 3, an integer

  • 2.0, an integer

Reveal answer
Answer:

2.5, a floating-point result

In Python 3, / always returns a float, even when both operands are integers.

5integer operand2.5floating-point result/true division2integer operandproduces
What value and output type result when two integers are divided with /?

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.

Definition

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.

Python 2historical divisionbehavior5 / 2same expression2.5Python 3 floating-pointresultPython 3/ always returns a floatinterpretsinterpretsin Python 3
How should you understand the historical difference between Python 2 and Python 3 for the same division expression?

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.

Worked example

Selecting the Integer Result

You need the floored result of dividing 5 by 2. Which operator should you choose, and what result follows?

  1. 1
    Identify the required result

    The requirement is an integer result rather than the exact floating-point quotient.

  2. 2
    Select the operator

    Use // because Python defines it as floored division.

  3. 3
    Evaluate the expression

    The expression 5 // 2 gives 2 because 2 is the value reached by rounding the quotient down toward negative infinity.

Result

Use 5 // 2 when the required result is the floored integer 2.

5 / 2floating-point division2.5floating-point result5 // 2floored division2integer resultproducesfloors to
What changes when the operator is changed from / to //?

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?

  • -2, because the fractional part is removed

  • -3, because floored division moves toward negative infinity

  • 2, because the sign is ignored

  • -2.5, because // returns a floating-point quotient

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.

-2.5quotient before flooring-3floored result0direction not used byflooringfloor toward negative infinity
How does -5 // 2 relate to the number line when flooring moves toward negative infinity?

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.

Desired resultchoose by requirementFloating-pointquotientchoose //always returns a float inPython 3Floored integerchoose ////floors toward negativeinfinityneed quotientneed floored integerselectselect
Which operator should be selected for the desired result?
RequirementOperatorResult 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

  • Common mistake

    Assuming that two integer operands force an integer result.

    What it looks like

    5 / 2 is treated as though it must produce an integer.

    Why it fails

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

    Do this instead

    Use // when a floored integer result is required.

  • Common mistake

    Calling / truncating division.

    What it looks like

    7 / 3 is expected to become an integer by dropping its fractional part.

    Why it fails

    Python 3's / operator returns a floating-point result rather than truncating the quotient to an integer.

    Do this instead

    Use / for the floating-point quotient and // for floored division.

  • Common mistake

    Treating // as truncation toward zero.

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

    Floored division rounds toward negative infinity.

    Do this instead
    Expect -5 // 2 to equal -3.
  • Common mistake

    Choosing an operator based only on the operand types.

    What it looks like

    The operands are integers, so // is selected automatically even when the exact floating-point quotient is needed.

    Why it fails

    The required result, not just the operand type, determines the appropriate operator.

    Do this instead

    Choose / for a floating-point quotient and // for a floored integer result.

7left operand2.333...floating-point quotient/returns a float3right operanddetermines result kind
How can the operands and operator be mapped to the resulting value and data type before execution?

Check Your Prediction

MEDIUM

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

Practice Answers

Determine the behavior of 9 / 2, 9 // 2, and -9 // 2.

  1. 1
    Evaluate 9 / 2

    The / operator returns a floating-point quotient, so the result is 4.5.

  2. 2
    Evaluate 9 // 2

    The // operator performs floored division. For this positive quotient, the floored result is 4.

  3. 3
    Evaluate -9 // 2

    The quotient lies between -4 and -5, and flooring moves toward negative infinity, so the result is -5.

Result

The expressions produce 4.5, 4, and -5 respectively.

Essential Rules

  1. 1
    In Python 3, / always returns a floating-point result, even when both operands are integers.
  2. 2
    Use / when the exact floating-point quotient is the required kind of result.
  3. 3
    Use // when a floored integer result is required.
  4. 4
    Floored division rounds toward negative infinity, so -5 // 2 equals -3 rather than -2.
  5. 5
    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.

Related concepts

More concepts starting with I