Concepts / Data Types: Numbers

Data Types: Numbers

The first three perform familiar whole-number or decimal calculations.

  • Programming

Why Results Surprise Beginners

Arithmetic expressions look familiar because Python uses the same basic mathematical symbols for addition, subtraction, multiplication, division, and exponentiation. The main source of surprise is often division: in Python 3, / always produces a floating-point result, while // performs floor division. When an expression produces an unexpected value, tracing each operation in order makes the difference visible.

Five Operators at a Glance

Python provides five basic arithmetic operators. The first three perform familiar whole-number or decimal calculations. The fourth, /, performs true division and always returns a float in Python 3. The fifth, **, raises a value to a power.

OperatorOperationGenerated exampleResult
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/True division7 / 23.5
**Exponentiation2 ** 38

The five basic arithmetic operators in Python. The examples and results are generated teaching examples.

python
Output
addition: 10
subtraction: 4
multiplication: 21
division: 3.5
power: 8

Tracing Division Choices

The operators / and // answer different questions. True division asks for the quotient as a floating-point result. Floor division asks for the result after rounding down to the nearest integer. Rounding down means moving toward the lower integer, which matters especially when the result is negative.

7 / 23.5-7 / 2-3.57 // 23-7 // 2-4same operands, different operatorsame operands, different operator
What result does each division operator produce for positive and negative operands, and how do their outputs differ?
Worked example

Choosing the division operator

Compare 9 / 4 with 9 // 4.

  1. 1
    Evaluate true division

    The expression 9 / 4 produces 2.25 because Python 3's / operator returns a floating-point result.

  2. 2
    Evaluate floor division

    The expression 9 // 4 produces 2 because floor division rounds the result down to the nearest integer.

  3. 3
    Compare the results

    The operands are the same, but the operators request different kinds of division.

Result

9 / 4 is 2.25, while 9 // 4 is 2.

python
Output
2.25
2
-2.25
-3

Following Evaluation Order

Python follows the standard mathematical order of operations when evaluating arithmetic expressions. An expression containing several operators is not necessarily evaluated from left to right. Use parentheses when you want to override the usual precedence or make your intended grouping clear.

2 + 3 * 4mixed expression3 * 4122 + 121414final resultmultiplication firstthen additionevaluate
In what order does Python evaluate a mixed arithmetic expression, and at which step can the result diverge from an expected value?
Worked example

Parentheses change the grouping

Compare 2 + 3 * 4 with (2 + 3) * 4.

  1. 1
    Evaluate 2 + 3 * 4

    Multiplication is evaluated before addition, so 3 * 4 becomes 12. The remaining expression is 2 + 12, which produces 14.

  2. 2
    Evaluate (2 + 3) * 4

    The parentheses require 2 + 3 to be evaluated first. That produces 5, and 5 * 4 produces 20.

  3. 3
    Identify the divergence

    The results differ because the parentheses change which operation is performed first.

Result

2 + 3 * 4 produces 14, while (2 + 3) * 4 produces 20.

Code walkthrough
result = 10 - 2 * 3 + 8 / 4
  1. 1
    Start with the multiplication: 2 * 3 produces 6.
  2. 2
    Evaluate the division: 8 / 4 produces 2.0 because / returns a float in Python 3.
  3. 3
    The expression is now 10 - 6 + 2.0.
  4. 4
    Evaluate from left to right among the remaining operations: 10 - 6 produces 4.
  5. 5
    Add 2.0 to produce the final result 6.0.
  • 10 - 2 * 3 + 8 / 4
  • 10 - 6 + 8 / 4
  • 10 - 6 + 2.0
  • 4 + 2.0
  • 6.0
Output
6.0

Mistakes Beginners Make

  • Common mistake

    Expecting / to return an integer when both operands are integers.

    What it looks like
    5 / 2
    Why it fails

    In Python 3, / always returns a floating-point result, so the result is 2.5 rather than an integer.

    Do this instead

    Use // when you need floor division and an integer result.

  • Common mistake

    Treating // as truncation toward zero.

    What it looks like
    -7 // 2
    Why it fails

    Floor division rounds down to the nearest integer. The lower integer is -4, not -3.

    Do this instead

    For negative results, check which integer is lower on the number line.

  • Common mistake

    Reading a mixed expression strictly from left to right.

    What it looks like
    2 + 3 * 4
    Why it fails

    Python follows standard mathematical order of operations, so multiplication is evaluated before addition.

    Do this instead

    Trace the higher-precedence operation first, or add parentheses to make the intended order explicit.

  • Common mistake

    Assuming Python 2 division expectations automatically apply to Python 3.

    Why it fails

    The source identifies division behavior as a difference between Python 2 and Python 3, and Python 3 defines / as always returning a float.

    Do this instead

    Identify the Python version and apply Python 3's true-division rule when working in Python 3.

Best practice

When an arithmetic result looks wrong, write the expression one step at a time. Mark each division operator, apply precedence before moving to lower-precedence operations, and use parentheses when the intended grouping is not immediately clear.

Check Your Reasoning

What do you think happens?

What do you think Python 3 produces for 6 / 3 and 6 // 3?

  • Both produce 2

  • The first produces 2.0 and the second produces 2

  • The first produces 2 and the second produces 2.0

  • Both produce 2.0

Reveal answer
Answer:

The first produces 2.0 and the second produces 2.

Python 3's / operator always returns a float, even when the division is exact. The // operator performs floor division and returns an integer.

MEDIUM

Trace the expression 18 - 2 ** 3 + 9 // 4. Write the intermediate expression after evaluating the exponentiation, then evaluate the floor division, and finally complete the remaining addition and subtraction.

Hints
  • Evaluate the exponentiation before the addition and subtraction.
  • Evaluate 9 // 4 as floor division.
  • After the higher-precedence operations are complete, work through the remaining expression from left to right.
Worked example

Practice solution

Trace 18 - 2 ** 3 + 9 // 4.

  1. 1
    Evaluate exponentiation

    2 ** 3 produces 8, leaving 18 - 8 + 9 // 4.

  2. 2
    Evaluate floor division

    9 // 4 produces 2, leaving 18 - 8 + 2.

  3. 3
    Complete the remaining operations

    18 - 8 produces 10, and 10 + 2 produces 12.

Result

The expression produces 12.

Key Takeaways

  1. 1
    Python's five basic arithmetic operators are +, -, *, /, and **.
  2. 2
    In Python 3, / always returns a floating-point result, even when the division is exact.
  3. 3
    The // operator performs floor division and rounds down to the nearest integer.
  4. 4
    Python follows standard mathematical order of operations, and parentheses override the usual grouping.
  5. 5
    Tracing each operation in order helps reveal why an arithmetic expression differs from an expectation.

At a glance

  • Use +, -, *, /, and ** for Python's five basic arithmetic operations.
  • Use / for true division and remember that Python 3 returns a float.
  • Use // for floor division, which rounds down to an integer.
  • Apply standard mathematical precedence and use parentheses to control grouping.
  • Trace expressions step by step when the result does not match your expectation.

Related concepts

More concepts starting with D