For AI agents: use /llms.txt for the Nakafa content index.
Arithmetic operators are symbols that allow you to perform mathematical operations in Python. Think of operators like tools in a toolbox. Each tool has a specific function, and you need to know when and how to use them to get the right results.
Python provides seven main arithmetic operators that you can use to perform mathematical calculations. Each operator has a different order of precedence, so it's important to understand which one will be executed first.
Python has seven arithmetic operators grouped into three precedence levels. This order determines which operator will be executed first in an expression.
Exponentiation Operator **
The ** operator is used for exponentiation and has the highest precedence among the seven binary arithmetic operators discussed here. Unlike the others, it is right-associative.
The exponentiation operator is very useful for various calculations. You can use it to calculate squares, cubes, square roots, or even negative powers to get fractions.
The group of operators with middle precedence consists of four operators that you often use in daily calculations.
Multiplication Operator
The * operator is used to multiply two numbers.
Division Operator
The / operator performs division and always produces a float result, even when both operands are integers.
Note that even though 4 / 2 produces a whole number mathematically, Python still returns as a float.
Floor Division Operator
The // operator performs floor division. It rounds the quotient down to the greatest integer that is less than or equal to the exact result.
Floor division rounds toward negative infinity. For a negative quotient such as -7 // 2, the result is -4, not -3. If either operand is a float, Python still applies the floor operation but returns a float.
Modulo Operator %
The % operator returns the remainder of division. This operator follows the mathematical formula .
For negative numbers, remember that Python keeps the identity above. That is why 5 % 4 produces 1, while -5 % 4 produces 3. Modulo is useful for divisibility checks and, for nonnegative integers, for finding the last digit.
Addition and Subtraction Operators
The + and - operators have the lowest precedence among all arithmetic operators.
Python evaluates expressions based on three main rules.
Associativity determines the evaluation order when there are multiple operators with the same precedence in one expression.
Left Associativity
Evaluation starts from left to right. Most operators use this rule.
The expression 6 / 2 * 4 is evaluated as (6 / 2) * 4 = 3 * 4 = 12, not 6 / (2 * 4) = 6 / 8 = 0.75.
Right Associativity
Evaluation starts from right to left. Only the exponentiation operator ** uses this rule.
The expression 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2) = 2 ** 9 = 512, not (2 ** 3) ** 2 = 8 ** 2 = 64.
Python shell is very effective as a calculator for various mathematical calculations. You can type mathematical expressions directly and Python will evaluate them in real-time.
Important things to remember are that the / operator always produces a float even when the result is a whole number. Conversely, the // operator produces an integer unless one of the operands is a float. For the modulo operator, the result will follow the sign of the divisor and use a consistent mathematical formula.
| Precedence | Operator | Function | Associativity |
|---|---|---|---|
| Highest | ** | Exponentiation | Right |
| Middle | * | Multiplication | Left |
| Middle | / | Division | Left |
| Middle | // | Floor division | Left |
| Middle | % | Modulo | Left |
| Lowest | + | Addition | Left |
| Lowest | - | Subtraction | Left |
By understanding arithmetic operators and their precedence order, you can write accurate and efficient mathematical expressions in Python programs. Use parentheses when you want to change the standard evaluation order to get the desired results.
Published: . Updated: .
>>> 2**3
8
>>> 2**-1
0.5
>>> 2**0.5
1.4142135623730951>>> 7.0 / 2.0
3.5
>>> 7.0 / 2
3.5
>>> 7 / 2.0
3.5
>>> 7 / 2
3.5
>>> 4 / 2
2.0>>> 4 // 2
2
>>> 7 // 2
3
>>> -7 // 2
-4
>>> 7.0 // 2
3.0>>> 5 % 4
1
>>> -5 % 4
3
>>> 5 % -4
-3
>>> -5 % -4
-1>>> 2 + 4 * 3
14
>>> (2 + 4) * 3
18
>>> 2 * 2**3
16
>>> 6 / 2 * 4
12
>>> 6 / (2 * 4)
0.75>>> 6 / 2 * 4
12
>>> 10 - 3 - 2
5>>> 2 ** 3 ** 2
512
>>> 3 ** 2 ** 2
81