Order of Operations in python is also known as operator precedence.
For evaluation of mathematical expression rule of operator precedence plays important role.
The acronym PEMDAS is simple way to remember rules for evaluation :
P – Parenthesis
E – Exponentiation
M – Multiplication
D – Division
A – Addition
S – Subtraction
Highest Precedence operator in Parenthesis and lowest precedence operator is OR
Table for operator precedence
NOTE: If paranthesis is not considered as an operator than the highest precedence is given to Exponent operator **
Examples for operator precedence:
>>> a or b + 20
>>> c or a + 20
>>> c and a + 20
>>> a and b + 20
1. If there are multiple operators of same group then associativity is vital.
2. Associativity decides the order of operators having same precedence value.
3. Left-to-right associability rule is followed by all operators.
4. Example 143-43+50, in this – and + both belongs to same group. So as per Left-to-right associability rule first subtraction will be performed and then addition.
Non Associative operators
1. As there is no associability for python assignment operator and comparison operators separate rules for sequencing are present for such operators.
2. Example a>b>c ,it is simplified as a>b and b>c then evaluates from left-to-right pattern.