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:
-
Parentheses:
() -
Exponentiation:
** -
Unary operators:
+x,-x,~x -
Multiplication and division:
*,/,//,% -
Addition and subtraction:
+,- -
Bit shifts:
<<,>> -
Bitwise operators:
&,^,| -
Comparisons, identity and membership:
==,!=,<,is,in -
Logical
not -
Logical
and -
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.