Python Basics — Arithmetic Operators
Arithmetic Operators
Arithmetic operators perform mathematical calculations. Python supports all the standard math operations plus a few extras.
Operator Reference
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 10 - 4 |
6 |
* |
Multiplication | 3 * 7 |
21 |
/ |
Division (true) | 7 / 2 |
3.5 |
// |
Floor division | 7 // 2 |
3 |
% |
Modulo (remainder) | 7 % 3 |
1 |
** |
Exponentiation | 2 ** 10 |
1024 |
Addition and Subtraction
print(10 + 3) # 13
print(10 - 3) # 7
print(-5 + 3) # -2
print(2.5 + 1.3) # 3.8
The + operator also works with strings (concatenation) and lists:
print("Hello" + " " + "World") # Hello World
print([1, 2] + [3, 4]) # [1, 2, 3, 4]
Multiplication
print(4 * 5) # 20
print(3 * 2.5) # 7.5
print(-2 * 3) # -6
The * operator also repeats strings and lists:
print("Ha" * 3) # HaHaHa
print([0] * 5) # [0, 0, 0, 0, 0]
Division: / vs //
Python has two division operators — understanding the difference is critical.
True Division (/)
Always returns a float, even when dividing two integers:
print(10 / 3) # 3.3333333333333335
print(10 / 2) # 5.0 (float, not int!)
print(7 / 2) # 3.5
Floor Division (//)
Returns the result rounded down to the nearest integer:
print(10 // 3) # 3
print(7 // 2) # 3
print(10 // 2) # 5
# With negative numbers — rounds toward negative infinity
print(-7 // 2) # -4 (not -3!)
print(-10 // 3) # -4
# Floor division with floats returns a float
print(7.0 // 2) # 3.0
print(7 // 2.0) # 3.0
Modulo Operator (%)
Returns the remainder after division:
print(10 % 3) # 1 (10 ÷ 3 = 3 remainder 1)
print(15 % 5) # 0 (15 ÷ 5 = 3 remainder 0)
print(7 % 2) # 1
Common Use Cases
# Check if a number is even or odd
number = 42
if number % 2 == 0:
print(f"{number} is even") # 42 is even
else:
print(f"{number} is odd")
# Check divisibility
if 15 % 3 == 0:
print("15 is divisible by 3") # 15 is divisible by 3
# Wrap around (clock arithmetic)
hours = 25
print(hours % 24) # 1 (25 hours = 1 AM next day)
Exponentiation (**)
Raises a number to a power:
print(2 ** 3) # 8 (2 × 2 × 2)
print(5 ** 2) # 25 (5 × 5)
print(10 ** 0) # 1 (anything to the power of 0)
print(2 ** 10) # 1024
# Square root using **
print(25 ** 0.5) # 5.0
print(27 ** (1/3)) # 3.0 (cube root)
Order of Operations (PEMDAS)
Python follows standard mathematical precedence:
| Priority | Operator | Operation |
|---|---|---|
| 1 (highest) | ** |
Exponentiation |
| 2 | +x, -x |
Unary plus/minus |
| 3 | *, /, //, % |
Multiplication, division, modulo |
| 4 (lowest) | +, - |
Addition, subtraction |
# Without parentheses — follows PEMDAS
print(2 + 3 * 4) # 14 (not 20)
print(2 ** 3 ** 2) # 512 (3**2 = 9, then 2**9 = 512)
print(10 - 2 * 3 + 1) # 5
# With parentheses — override precedence
print((2 + 3) * 4) # 20
print((10 - 2) * (3 + 1)) # 32
Tip: When in doubt, use parentheses. They make your code clearer and prevent mistakes.
The divmod() Function
Returns both the quotient and remainder as a tuple:
quotient, remainder = divmod(17, 5)
print(f"17 ÷ 5 = {quotient} remainder {remainder}")
# 17 ÷ 5 = 3 remainder 2
# Useful for time conversions
total_minutes = 135
hours, minutes = divmod(total_minutes, 60)
print(f"{total_minutes} minutes = {hours}h {minutes}m")
# 135 minutes = 2h 15m
Common Mistakes
| Mistake | Example | Fix |
|---|---|---|
/ returns float |
10 / 2 → 5.0 |
Use // for integer result |
| Negative floor division | -7 // 2 → -4 |
Rounds toward -∞, not zero |
| Division by zero | 10 / 0 → ZeroDivisionError |
Check divisor first |
| Operator precedence | 2 + 3 * 4 → 14 |
Use parentheses: (2 + 3) * 4 |
Summary
+,-,*work as expected for basic math/(true division) always returns a float//(floor division) rounds down to nearest integer%(modulo) returns the remainder — great for even/odd checks**for exponentiation (powers and roots)- Use parentheses to control order of operations
- Use
divmod()when you need both quotient and remainder
Next up: Comparison Operators →