Arithmetic Operators
Perform math with +, -, *, /, //, % and **.
Syntax
a + b
a // b
a ** bArithmetic operators perform calculations on numbers.
| Operator | Meaning |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division (always a float) |
// | Floor division (whole number) |
% | Remainder (modulus) |
** | Exponentiation |
Example
print(7 + 3)
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 8)
# Output:
# 10
# 3.5
# 3
# 1
# 256When to use it
- A checkout system multiplies item price by quantity and adds tax to compute the order total.
- A password generator uses modulo to wrap a random index within the character set length.
- A physics simulation uses the exponent operator ** to compute kinetic energy (0.5 * m * v**2).
More examples
Basic arithmetic operations
Demonstrates all seven Python arithmetic operators on two integer values.
a = 17
b = 5
print(a + b) # 22
print(a - b) # 12
print(a * b) # 85
print(a / b) # 3.4
print(a // b) # 3 (floor division)
print(a % b) # 2 (remainder)
print(a ** b) # 1419857 (power)Augmented assignment operators
Uses augmented assignment shortcuts (+=, -=, *=, //=) to update a running total.
total = 100
total += 20 # total = 120
total -= 10 # total = 110
total *= 2 # total = 220
total //= 3 # total = 73
print(total) # 73Order of operations
Shows Python's operator precedence: ** > * > + and how parentheses override it.
result = 2 + 3 * 4 ** 2
print(result) # 50 (** first, then *, then +)
result2 = (2 + 3) * 4 ** 2
print(result2) # 80 (parentheses force + first)
Discussion