Using Operators for Arithmetic
Arithmetic operators tell Python which calculation to perform. For example, + adds values, * multiplies them, and ** raises one value to a power.
Python provides seven main arithmetic operators. Their precedence determines how an expression is grouped when parentheses do not specify the grouping.
Operator Precedence Order
When one expression mixes several arithmetic operators, Python needs a fixed order to decide which calculation happens first. These seven binary operators fall into three precedence levels. Precedence determines how operations are grouped. Parentheses can change that grouping.
Highest Precedence
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.
>>> 2**3
8
>>> 2**-1
0.5
>>> 2**0.5
1.4142135623730951Use ** to calculate squares, cubes, roots, and negative powers. For example, a square root can be written with the exponent 0.5.
Middle Precedence
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 produces a float result for int or float operands, even when both are integers.
>>> 7.0 / 2.0
3.5
>>> 7.0 / 2
3.5
>>> 7 / 2.0
3.5
>>> 7 / 2
3.5
>>> 4 / 2
2.0Even though 4 / 2 is a whole number mathematically, Python 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.
>>> 4 // 2
2
>>> 7 // 2
3
>>> -7 // 2
-4
>>> 7.0 // 2
3.0Floor division rounds toward negative infinity. For -7 // 2, Python gets -3.5 and rounds down to -4. 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. For integer operands and a nonzero divisor, it follows the exact identity .
>>> 5 % 4
1
>>> -5 % 4
3
>>> 5 % -4
-3
>>> -5 % -4
-1For negative integers, Python still satisfies 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.
Lowest Precedence
Addition and subtraction have lower precedence than the other five operators. Parentheses can still change the grouping of operations. In 2 + 3 * 4, Python multiplies first and adds afterwards, so the result is 14. To make the addition happen earlier, wrap it in parentheses and write (2 + 3) * 4.
Precedence Rules and Associativity
Precedence and associativity determine how operations are grouped, while Python evaluates operands from left to right. Precedence groups operators from different levels, and associativity groups operators that sit on the same level. Both rules act on the operators you actually wrote, so 2 ** 3 ** 2 and 8 - 3 - 2 each have exactly one reading.
Expression Evaluation Order
Three rules determine how operations are grouped within an expression.
- Precedence order determines which operators bind more tightly
- Left associativity applies to operators with the same precedence, except exponentiation
- Parentheses can change the evaluation order
>>> 2 + 4 * 3
14
>>> (2 + 4) * 3
18
>>> 2 * 2**3
16
>>> 6 / 2 * 4
12.0
>>> 6 / (2 * 4)
0.75Associativity Rules
Associativity determines grouping at equal precedence within an expression.
Left Associativity
Most binary arithmetic operators at the same precedence level are grouped from left to right.
>>> 6 / 2 * 4
12.0
>>> 10 - 3 - 2
5Multiplication and division have the same priority, so Python reads this expression from left to right: 6 / 2 * 4 = (6 / 2) * 4 = 12.
Right Associativity
Among these seven operators, exponentiation ** is grouped from right to left.
>>> 2 ** 3 ** 2
512
>>> 3 ** 2 ** 2
81Python groups repeated exponentiation from right to left: 2 ** 3 ** 2 = 2 ** (3 ** 2) = 2 ** 9 = 512.
Python Shell as Calculator
For int or float operands, the / operator produces a float, even when the result is a whole number. The // operator produces an integer when both operands are integers and a float when either operand is a float. A nonzero modulo result has the same sign as the divisor. The identity shown above is exact for integers, while floating-point operations can introduce rounding.
Arithmetic Operators Summary Table
| 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 |
Operator precedence determines how Python groups an arithmetic expression. Use parentheses to override the standard grouping when the intended order differs.