Arithmetic Operators
Arithmetic Operators
Arithmetic Operators
In this section, we will get familiar with arithmetic operators in Python and see how they differ from what you might expect coming from mathematics or other languages.
Addition (+), Subtraction (-), Multiplication (*)
These three work exactly as in mathematics:
Division (/)
This is where Python differs from C++. In Python, / always returns a float, even if both numbers are integers and the result is whole:
Floor Division (//)
If you need integer division — the whole part only, fractional part discarded — use //:
The name "floor division" comes from the mathematical floor function — floor(x) means rounding down to the nearest integer on the number line. For positive numbers this feels natural: floor(3.75) = 3. But for negative numbers it may surprise you:
Mathematically, -7 / 2 = -3.5. The floor of -3.5 on the number line is -4, because -4 is the nearest integer that is less than or equal to -3.5. So Python gives -4, not -3. This is mathematically correct and consistent, but differs from C++ where integer division truncates toward zero and would give -3.
Modulo (%)
The modulo operator returns the remainder after division:
You will use % constantly in competitive programming — for checking if a number is even or odd, for cyclic indexing, and for keeping numbers within a range:
Exponentiation (**)
Python has a built-in operator for raising a number to a power:
There is no equivalent single operator in C++ — this is a Python convenience you will appreciate.
Operator Precedence
When an expression contains multiple operators, Python evaluates them in a specific order — just like in mathematics. From highest to lowest priority:
| Priority | Operator | Description |
|---|