Operator Precedence and Associativity of Operators

Operator Precedence and Associativity of Operators

Operator Precedence in Python

Operators are the key elements for performing any operation in any programming language.  Operators tell the equation about the operation to be performed. But what if we have a complex equation where more than one operator is paced between two operands and numbers of operands are more than 2. Ex equation = a+b-c*d/e. For these let’s have a look at Operator Precedence and Associativity of Operators.

Different operators and their precedence

S.noOperatorsName of the operatorAssociativity order
1(, )Brackets, ParenthesisLeft to Right
2**ExponentRight to left
3~Bit-wise NOTLeft to Right
4*, /, %, //Multiplication, Division, Modulo, Floor divisionLeft to Right
5+, –Addition, SubtractionLeft to Right
6>>, <<Bit-wise right and left shiftLeft to Right
7&Bit-wise ANDLeft to Right
8^Bit-wise XORLeft to Right
9|Bit-wise ORLeft to Right
10=, !=, >, <, >=, <=Comparison OperatorLeft to Right
11=, +=, -=, *=, /=, %=, **=, //=Assignment OperatorRight to left
12is, is notIdentity OperatorLeft to Right
13in, not inMembership OperatorLeft to Right
14and, or, notLogical OperatorLeft to Right

-> Above table shows the precedence and associativity of the operators, Precedence of the operator decrease as we move down in the table.

Precedence in operators

Precedence of the operator is to define which operator will be given higher priority to solve the equation. If in an equation more than one operator is present than the above table will be referred to perform operations. Different operators have different precedence and different usage. Advantages of following standard operator precedence table are:

  • Same result.
  • Follow universal grammar
  • Simplicity
  • Powerful approach

But what if we have 2 operators of the same precedence. These where associativity comes in the game.

Precedence

Associativity in Operators

Operator Associativity in python

Associativity of the operator is defined as in what order the operator is given Precision table should be used. Suppose in an equation we have both addition and subtraction operator, then which of the will be given higher precedence. Ex a+b-c, Some advantaged of following associativity table are:

  • Simplify operator usage
  • Powerful approach
  • Universal fact

To understand this we refer the associativity table so we can solve more complex equations by following global approach.

FAQ

How does precedence work in Python?

When python code is interpreted, the interpreter evaluates the operator with the highest precedence first. To check for the precedence of the operator one should refer to the precision table of operators.

Which operator has highest priority?

Operators present at the top of the precision table are considered as the operator with the highest precision. And if the operator lies under the same precedence then we refer the associativity table

Does Python use Bodmas?

Every mathematical operation is done by using mathematical rules. BODMAS [Brackets Order Division Multiplication Addition Subsraction]

What is the associativity of operators with the same precedence in Python?

Operators having the same precedence are differentiated by referring associative table. Suppose in an equation both addition and subtraction are used, then the addition is performed first and then the subtraction of operands is performed.