Python Arithmetic Operators: Making Your Code Do the Math
Computers exist to compute — and Python makes math effortless. Whether you're calculating a discount, finding how many items fit in a box, or converting temperatures, arithmetic operators are the tools you reach for. Learning them puts you in control of numerical data, which shows up in virtually every program you'll write.
The 7 Arithmetic Operators in Python
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 3 | 10 |
- | Subtraction | 7 - 3 | 4 |
* | Multiplication | 7 * 3 | 21 |
/ | Division | 7 / 2 | 3.5 (always float) |
% | Modulus (remainder) | 7 % 3 | 1 |
** | Exponentiation | 2 ** 8 | 256 |
// | Floor division | 7 // 2 | 3 (rounds down) |
Basic Usage: Numbers and Variables
All operators work with both raw numbers and variables. Python substitutes the variable's value before computing:
price = 49.99
quantity = 3
total = price * quantity
print(total) # 149.97
discount = total * 0.10
final = total - discount
print(round(final, 2)) # 134.97
Division vs. Floor Division
This is the most important distinction to memorize:
# Regular division — always returns a float:
print(10 / 3) # 3.3333333333333335
print(6 / 2) # 3.0 (still a float, even when exact!)
# Floor division — returns an integer (rounds toward -infinity):
print(10 // 3) # 3
print(7 // 2) # 3
print(-7 // 2) # -4 (rounds down, not toward zero)
Use // when you need a whole number — for example, figuring out how many full rows fit in a grid.
The Modulus Operator: Finding Remainders
The % operator returns what's left over after division. It's used far more often than you'd expect:
# Is a number even or odd?
print(10 % 2) # 0 → even (no remainder)
print(7 % 2) # 1 → odd
# How many candies left after splitting equally?
candies = 29
kids = 4
leftover = candies % kids
print(leftover) # 1 candy left over
# What minute is it? (convert seconds to mm:ss)
total_seconds = 137
minutes = total_seconds // 60
seconds = total_seconds % 60
print(f"{minutes}m {seconds}s") # 2m 17s
Exponentiation: Powers Made Easy
print(2 ** 10) # 1024 (2 to the power of 10)
print(3 ** 3) # 27 (3 cubed)
print(9 ** 0.5) # 3.0 (square root)
# Compound interest formula: A = P(1 + r)^t
principal = 1000
rate = 0.05
years = 10
amount = principal * (1 + rate) ** years
print(round(amount, 2)) # 1628.89
Order of Operations (PEMDAS)
Python follows standard math order of operations. Use parentheses to be explicit:
print(2 + 3 * 4) # 14 (multiplication first)
print((2 + 3) * 4) # 20 (parentheses first)
# Calculate average — parentheses matter!
a, b, c = 10, 20, 30
wrong = a + b + c / 3 # 40.0 (divides only c by 3)
correct = (a + b + c) / 3 # 20.0
Common Mistakes to Avoid
- Division by zero:
x / 0raises aZeroDivisionError. Always validate denominators before dividing. - Integer vs. float confusion:
7 / 2gives3.5, not3. Use//if you need a whole number. - Forgetting operator precedence: When in doubt, add parentheses to make your intent clear.
# Division by zero — guard against it:
divisor = 0
if divisor != 0:
print(10 / divisor)
else:
print("Cannot divide by zero")
Key Takeaways
+,-,*,/work as expected;/always returns a float.//performs floor division (integer result);%returns the remainder.**raises a number to a power (also works for square roots with0.5).- Use parentheses to control order of operations and make code readable.
What to Learn Next
Now that you can do math in Python, learn augmented assignment operators (+=, -=, *=) to update variables more concisely. Then explore comparison operators to start making decisions in your programs with if statements.
