Concepts / Data Types: Numbers

Data Types: Numbers

Python provides five basic arithmetic operators: +, -, *, /, and **

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

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

Choosing the division operator

Compare 9 / 4 with 9 // 4.

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

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

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

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.

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

Parentheses change the grouping

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

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

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

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

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

result = 10 - 2 * 3 + 8 / 4

Mistakes Beginners Make

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

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

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

  • Treating // as truncation toward zero.

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

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

  • Reading a mixed expression strictly from left to right.

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

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

  • Assuming Python 2 division expectations automatically apply to Python 3.

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

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

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.

Practice solution

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

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

Evaluate floor division: 9 // 4 produces 2, leaving 18 - 8 + 2.

Complete the remaining operations: 18 - 8 produces 10, and 10 + 2 produces 12.

The expression produces 12.

Key Takeaways

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

Key Takeaways

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