Python Arithmetic Operators
Satellite · Operators
Python Arithmetic Operators
Arithmetic is the foundation of expression building. Learn how each operator behaves, including floor division and exponent nuances.
Operator overview
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | total = a + b |
- | Subtraction | balance = income - expenses |
* | Multiplication | area = width * height |
/ | True division | 3 / 2 == 1.5 |
// | Floor division | 3 // 2 == 1 |
% | Modulo (remainder) | 5 % 2 == 1 |
** | Exponentiation | 2 ** 3 == 8 |
Examples
hours = total_minutes // 60
minutes = total_minutes % 60
compound_interest = principal * (1 + rate) ** years
Pitfalls
/always returnsfloat, even when dividing integers.%follows the divisor's sign:-5 % 2 == 1.- Use
math.iscloseto compare floats due to precision issues.