Perform calculations with integers and floats
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds two numbers | 3 + 2 → 5 |
- | Subtraction | Subtracts right side from left | 5 - 1 → 4 |
* | Multiplication | Multiplies numbers | 4 * 2 → 8 |
/ | Division | True division (float) | 5 / 2 → 2.5 |
// | Floor Division | Divides & rounds down | 5 // 2 → 2 |
% | Modulus | Remainder of division | 5 % 2 → 1 |
** | Exponentiation | Power operator | 2 ** 3 → 8 |
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
x = 7.5
y = 2.0
print(x * y) # 15.0
print(x / y) # 3.75
print(x // y) # 3.0 (still float)
print(x % y) # 1.5
Order of evaluation (highest → lowest): **
, unary +
/-
, * / // %
, + -
. Use parentheses for clarity.
result = 3 + 2 * 4 # 11
result = (3 + 2) * 4 # 20
result = 2 ** 3 ** 2 # 512 (evaluates right‑to‑left)
//
for integer division when you need a whole‑number result.0.1 + 0.2
issue); use decimal
for financial calcs.abs()
, round()
, pow()
, and divmod()
.print(abs(-7)) # 7
print(round(3.14159, 2)) # 3.14
print(pow(2, 5)) # 32 (same as 2 ** 5)
print(divmod(17, 5)) # (3, 2) → quotient & remainder