In this tutorial, you will learn about python operators precedence and operators precedence examples. When two operators share an operand, the operator with higher precedence goes first then other. Python has well defined rule for specifying order in which operators are evaluated when they have several operators. Basically multiplication and division has higher precision than Addition and Subtraction. If the two operators share an operand and if the operators have same precedence then the expression is evaluated by associativity of operators.
Now the explanation is as ** operator has right-to-left associativity, A ** B ** C is treated as A ** (B ** C). On the other hand, since the / operator has left-to-right associativity, A / B / C is treated as (A / B) / C.
e.g.:
The multiplication has higher precedence than addition, a+b*c is treated as a+ (b*c) and a*b+c is treated as (a*b) +c.
Below is the list of highest precedence to lower precedence:-
Precedence |
Operator |
Description |
1 |
** |
Exponent |
2 |
*, /, %, // |
multiply, divide, modulo, floor division |
3 |
+, - |
plus, minus |
4 |
>, <, >=, <= |
Comparison |
5 |
==, != |
Equality |
6 |
= %= /= //= -= += *= **= |
Assignment |
7 |
and, or, not |
Logical |