MWZ

MINDWAREZONE

beginnerPython

What is operator precedence?

Answer

Clear, interview-ready explanation

Operator precedence determines the order in which Python evaluates operators in an expression.

For example, multiplication has a higher precedence than addition:

result = 2 + 3 * 4
print(result)

// Output
14
            

Python evaluates 3 * 4 first and then adds 2.

A simplified precedence order from higher to lower is:

  1. Parentheses: ()
  2. Exponentiation: **
  3. Unary operators: +x, -x, ~x
  4. Multiplication and division: *, /, //, %
  5. Addition and subtraction: +, -
  6. Bit shifts: <<, >>
  7. Bitwise operators: &, ^, |
  8. Comparisons, identity and membership: ==, !=, <, is, in
  9. Logical not
  10. Logical and
  11. Logical or

Parentheses should be used when they make an expression’s intended order clearer:

result = (2 + 3) * 4
            

  Here, the addition is evaluated first, producing 20.