7.2 Arithmetic Operators
Python's arithmetic operators — addition, subtraction, multiplication, true division, floor division, modulus, and exponentiation — with a practical batch/leftover example.
The standard mathematical operators, all usable on int and float (and some on other types, see 7.13 Operators with Different Data Types).
Addition (+)
>>> 5 + 3
8
Subtraction (-)
>>> 5 - 3
2
Multiplication (*)
>>> 5 * 3
15
Division (/)
What Is It?
True division — always returns a float, even if the result is a whole number.
>>> 5 / 3
1.6666666666666667
Floor Division (//)
What Is It?
Divides and rounds down to the nearest whole number.
Why Is It Used?
When a whole-number result is needed — e.g. “how many full batches of 3 fit into 5 items?”
>>> 5 // 3
1
Modulus (%)
What Is It?
Returns the remainder of division.
Why Is It Used?
Extremely common for “every Nth item” logic, and checking even/odd.
>>> 5 % 3
2
Exponent (**)
>>> 5 ** 3
125
Practical Example
>>> total_items = 17
>>> batch_size = 5
>>> full_batches = total_items // batch_size
>>> leftover = total_items % batch_size
>>> full_batches, leftover
(3, 2)
Quick Interview Answer
“Python’s arithmetic operators are
+ - * / // % **. The one that trips people up most is/vs//:/is true division and always returns afloat, while//is floor division and rounds down to a whole number.%returns the remainder, which combined with//is the standard pattern for splitting a total into full batches plus a leftover.”
Common Mistakes
- Expecting
/to return anintwhen both operands areint— it always returns afloatin Python 3, unlike Python 2’s/, covered further in 7.14 Common Mistakes. - Confusing
//with rounding to the nearest whole number — it always rounds down (toward negative infinity), not to the nearest value. - Forgetting
**is right-associative —2 ** 3 ** 2is2 ** (3 ** 2)=512, not(2 ** 3) ** 2, detailed in 7.9 Operator Precedence and Expression Evaluation.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form