HOWTO · Python

Order of Operations in Python

This tutorial discusses the order of execution of operators in Python, covering operator precedence, arithmetic operators, logical operators, and the use of parentheses for clarity. Gain a solid understanding of how Python evaluates expressions to write better code and avoid common pitfalls.

On this page

Python evaluates operators according to precedence, so multiplication and division bind more tightly than addition and subtraction. To predict a result, group the expression by precedence, then account for associativity and any short-circuiting logical operators. Parentheses make the intended grouping explicit.

What is Operator Precedence?

Operator precedence is the priority Python uses to group parts of an expression. It describes grouping, not a universal left-to-right execution schedule: associativity determines how operators at the same precedence group, and Python evaluates operands in their specified order. For example, 3 + 4 * 2 groups as 3 + (4 * 2) and has the value 11.

The following reference lists common Python operators from higher to lower precedence. Operators on the same row share a precedence level; use the associativity rules or parentheses to determine their grouping.

Precedence, high to low Operators or expressions
Grouping and access (expressions), x[index], x(...), x.attribute
Await await x
Exponentiation **
Unary numeric and bitwise +x, -x, ~x
Multiplication *, @, /, //, %
Addition +, -
Shifts <<, >>
Bitwise AND &
Bitwise XOR ^
Bitwise OR `
Comparisons and membership/identity tests <, <=, >, >=, !=, ==, in, not in, is, is not
Boolean NOT not x
Boolean AND and
Boolean OR or
Conditional expression x if condition else y
Lambda lambda
Assignment expression :=

This is a practical reference for the operators discussed here, not a complete grammar of every expression form. PEMDAS is a familiar arithmetic mnemonic, but it does not describe all Python operators or replace the precedence rules above. Exponentiation also groups from right to left, so 2 ** 3 ** 2 means 2 ** (3 ** 2). Unary signs and exponentiation have a special relationship: -2 ** 2 groups as -(2 ** 2).

Basic Arithmetic Operators

In arithmetic, parentheses group first; exponentiation follows; then multiplication, division, floor division, and modulo; finally addition and subtraction. Multiplication and division have equal precedence and associate left to right, as do addition and subtraction. The / operator performs true division, so dividing integers can produce a float.

Run this example:

a = 10
b = 5
c = 2

mixed = a + b * c - a / b
explicit = (a + b) * c - a / b
print(mixed)
print(explicit)

The first expression groups as 10 + (5 * 2) - (10 / 5); the second groups as (10 + 5) * 2 - (10 / 5). The divisions produce floating-point values, so the final values are floats. This example has not been executed, so its output is not shown here.

The same rules explain 10 + 2 / 2: division is grouped first, so the value is 11. Python does not choose addition first simply because it appears first in the expression.

Using Parentheses for Clarity

Parentheses establish grouping and can change a result. In the example above, (a + b) is grouped before multiplication, which changes the arithmetic from the unparenthesized expression. Add parentheses when they clarify the calculation you intend, even when Python’s default precedence would already produce the same grouping.

Parentheses control grouping; they do not mean every part of the expression is evaluated eagerly. Python still follows the semantics of each operator, including the short-circuit behavior of and and or.

Comparison and Logical Operators

Comparisons such as ==, !=, <, <=, >, and >= have higher precedence than not, and, and or. Parentheses help show which comparisons feed a logical expression. For instance, (10 > 5) and (15 > 10) combines two true comparisons, so the expression is true.

not applies to the expression that follows it and binds more tightly than and; and binds more tightly than or. Both and and or short-circuit: Python may skip evaluating the right operand when the left operand determines the result. They also return one of their operands, rather than always returning a Boolean. In a conditional, Python treats the resulting value according to its truth value.

For a readable condition, write explicit groups such as (age >= 18) and (has_ticket or is_guest). This makes the intended relationship clear and helps prevent a precedence rule from being mistaken for the business rule.

Chaining Comparisons

Python supports comparison chains such as 10 > 5 > 15. This is equivalent in result to 10 > 5 and 5 > 15, with the middle expression evaluated only once. The first comparison is true and the second is false, so the whole chain is false. Python does not compare the Boolean result of 10 > 5 with 15.

A chain is useful for range checks: 0 <= score <= 100 requires both comparisons to be true. If the comparisons involve different expressions or the middle value should be evaluated more than once, write the intended comparisons explicitly and use a temporary variable where that improves clarity.

Conclusion

To predict a Python expression, apply the operator precedence rules, check associativity when operators share a level, and account for behavior such as short-circuiting. Use parentheses to communicate the grouping you intend. These habits make arithmetic and conditional expressions easier to read and debug.

FAQ

  1. What is operator precedence in Python?

    It is the priority Python uses to group operators in an expression. Associativity and each operator’s semantics also affect how the expression is evaluated.

  2. How can I change the grouping of an expression?

    Put parentheses around the subexpression that should be grouped first, such as (a + b) * c.

  3. What happens when operators have the same precedence?

    It depends on the operator. Most operators group left to right, exponentiation groups right to left, and comparisons can form a chain. Add parentheses when the grouping might be unclear.

  4. What if I leave out parentheses in a complex expression?

    Python applies its defined precedence and associativity rules. If that grouping is difficult to see, add parentheses to make the intended calculation clear.

  5. Can I chain comparison operators in Python?

    Yes. A chain such as 0 <= score <= 100 checks both comparisons and evaluates the middle expression once.