Arithmetic Operators in Python: All 7 Explained with Examples
Python's 7 arithmetic operators explained with code examples, operator precedence rules, and the edge cases most placement-exam candidates miss.
Python’s 7 arithmetic operators are simple to list and surprisingly easy to get wrong in placement coding tests if you skip the edge cases.
All 7 Python Arithmetic Operators
Per the Python 3 language reference on operator precedence, every arithmetic operator has a defined precedence rank. Here is the complete set:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 9 + 2 | 11 |
- | Subtraction | 9 - 2 | 7 |
* | Multiplication | 9 * 2 | 18 |
/ | True Division | 9 / 2 | 4.5 |
// | Floor Division | 9 // 2 | 4 |
% | Modulo | 9 % 2 | 1 |
** | Exponentiation | 9 ** 2 | 81 |
The most common confusion is between / and //. In Python 3, the / operator always returns a float, even when dividing two integers evenly. So 8 / 2 returns 4.0, not 4. Use // when your program needs an integer result: a loop counter, an index, a count of full batches.
For a hands-on application of all seven operators, the calculator program in Python guide builds a menu-driven calculator using each one.
Operator Precedence and Associativity
When an expression contains multiple operators, Python decides evaluation order using precedence. Higher-precedence operators run first.
Precedence Table (Highest to Lowest)
| Level | Operator(s) | Description |
|---|---|---|
| 1 | ** | Exponentiation |
| 2 | *, /, //, % | Multiplication, division, floor division, modulo |
| 3 | +, - | Addition, subtraction |
This is BODMAS/PEMDAS expressed in Python terms: exponentiation before multiplication and division before addition and subtraction.
Associativity
Most Python arithmetic operators evaluate left to right. The one exception is **, which evaluates right to left.
9 - 3 - 2evaluates as(9 - 3) - 2 = 4(left to right)2 ** 3 ** 2evaluates as2 ** (3 ** 2) = 2 ** 9 = 512(right to left)
The right-to-left rule for ** catches students who assume uniform left-to-right evaluation across all operators. If you expected (2 ** 3) ** 2 = 64, Python gives 512 instead.
Step-by-Step: Evaluating a Mixed Expression
Consider c = 9 + 2 - 9 * 2 / 9:
- Step 1:
9 * 2 = 18(multiplication, highest among remaining operators) - Step 2:
18 / 9 = 2.0(division, same precedence level as Step 1, left to right) - Step 3:
9 + 2 = 11(addition) - Step 4:
11 - 2.0 = 9.0(subtraction)
The result is 9.0, not 9. The / operator returns a float, so the entire expression becomes a float even though all the original values are integers.
Worked Examples in Python
Running all seven operators against the same two values makes the differences concrete:
a = 9
b = 2
print(a + b) # 11
print(a - b) # 7
print(a * b) # 18
print(a / b) # 4.5
print(a // b) # 4
print(a % b) # 1
print(a ** b) # 81
Each result matches the reference table. Running this program and verifying the output line by line is the fastest way to internalize all seven operators before a placement coding round.
For more programs that combine these operators in different ways, the Python basic programs for practice collection covers about forty problems ranging from addition to factorial to reversal.
Arithmetic operators also appear in array-level problems. The sum of array elements in Python guide applies + across a loop to accumulate a running total, which is a common sub-problem inside larger placement questions.
Arithmetic Operators with Strings and Lists
The + and * operators are not exclusive to numbers. They work on strings and lists too, doing different things in each context.
String Operations
"Hello" + " " + "World"returns"Hello World"(concatenation)"Ha" * 3returns"HaHaHa"(repetition)"Ha" + 3raisesTypeError: can only concatenate str (not "int") to str
Only + and * are valid for strings. Attempting -, /, //, %, or ** on a string raises a TypeError. This operator-type combination is a reliable placement test question, especially in MCQ rounds.
List Operations
[1, 2] + [3, 4]returns[1, 2, 3, 4](list concatenation)[0] * 5returns[0, 0, 0, 0, 0](list repetition)
The Python 3 documentation on numeric types explains how operator overloading works across built-in types.
What Does Not Work
Other arithmetic operators do not extend to strings or lists. If a placement test asks what "abc" - "a" returns, the answer is a TypeError, not "bc". Python has a dedicated str.replace() method for that kind of operation, not a subtraction operator.
Edge Cases That Matter in Placement Tests
Most mistakes with Python arithmetic operators come from four specific edge cases. Each of these has appeared in placement MCQ and coding rounds.
True Division Always Returns Float
In Python 3, / always returns a float.
9 / 3returns3.0, not38 / 2returns4.0, not4- Use
int(9 / 3)or9 // 3when your code requires an integer result
Students from C or Java backgrounds sometimes expect an integer result from dividing two integers. Python 3 changed this behaviour intentionally, and placement MCQ rounds test this regularly.
Negative Floor Division Rounds Toward Negative Infinity
Python’s // rounds toward negative infinity, not toward zero.
-9 // 2returns-5, not-4- The floor of
-4.5is-5(toward negative infinity) - In C, integer division of
-9 / 2truncates toward zero and returns-4
This distinction catches candidates who apply their C or Java intuition to Python floor division.
Negative Modulo Follows the Divisor’s Sign
Python’s modulo result carries the sign of the divisor, not the dividend.
-9 % 2returns1, not-1- Verify:
-9 = (-5) * 2 + 1, so the remainder is1 -9 % -2returns-1(divisor is negative, so result is negative)
Division by Zero Raises an Exception
9 / 0 and 9 // 0 both raise ZeroDivisionError at runtime. Python does not silently return infinity or NaN for integer division by zero. A standard guard pattern:
if b != 0:
result = a / b
else:
result = None # or raise a custom exception
The greatest of two numbers guide covers comparison patterns before performing arithmetic, which applies directly to guarding divisions.
Once all 7 operators and the precedence rules are clear, the natural step is writing programs that combine them: a GPA calculator, a score normaliser, a data formatter that converts between number bases.
TinkerLLM applies these same arithmetic operators in building practical tools, from salary estimators to prompt-token counters for AI APIs. If the operator table above is already familiar, the first module runs in under two hours. The entry point is ₹299.
Primary sources
Frequently asked questions
What is the difference between / and // in Python?
The / operator performs true division and always returns a float in Python 3, so 9 / 2 gives 4.5 and 8 / 2 gives 4.0 (not 4). The // operator performs floor division and returns the integer part of the quotient: 9 // 2 gives 4. Use // when your code needs an integer result.
What does the % (modulo) operator do in Python?
The % operator returns the remainder after division. For example, 9 % 2 returns 1 because 9 = 4*2 + 1. For negative numbers, Python's modulo follows the sign of the divisor: -9 % 2 returns 1, not -1.
Is the ** (exponentiation) operator right-associative?
Yes. In Python, 2**3**2 evaluates as 2**(3**2) = 2**9 = 512, not (2**3)**2 = 64. All other Python arithmetic operators evaluate left to right; ** is the only right-to-left exception.
Can arithmetic operators be used with strings in Python?
Yes, but only + and *. The + operator concatenates strings (e.g. 'Hello' + ' World' = 'Hello World'). The * operator repeats a string (e.g. 'Ha' * 3 = 'HaHaHa'). Using -, /, //, %, or ** on strings raises a TypeError.
What happens when you divide by zero in Python?
Dividing by zero with / or // raises a ZeroDivisionError at runtime. Python does not return infinity or NaN for integer division by zero. Guard any division with a conditional check or a try-except block when the denominator might be zero.
Does Python follow BODMAS or PEMDAS for operator precedence?
Both BODMAS and PEMDAS describe the same order of operations, and Python follows both conventions. Exponentiation runs first, then multiplication, division, floor division, and modulo from left to right, then addition and subtraction from left to right.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)