Data Types: Numbers
Python provides five basic arithmetic operators: +, -, *, /, and **
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.
| Operator | Operation | Generated example | Result |
|---|---|---|---|
| + | Addition | 7 + 3 | 10 |
| - | Subtraction | 7 - 3 | 4 |
| * | Multiplication | 7 * 3 | 21 |
| / | True division | 7 / 2 | 3.5 |
| ** | Exponentiation | 2 ** 3 | 8 |
addition: 10
subtraction: 4
multiplication: 21
division: 3.5
power: 8Tracing 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.
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.
2.25
2
-2.25
-3Following 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.
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?
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.
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
- Python's five basic arithmetic operators are +, -, *, /, and **.
- In Python 3, / always returns a floating-point result, even when the division is exact.
- The // operator performs floor division and rounds down to the nearest integer.
- Python follows standard mathematical order of operations, and parentheses override the usual grouping.
- 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.