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
<strong><span class="has-inline-color has-vivid-green-cyan-color">Order of Operations or Operator Precedence</span></strong>
NOTE: If paranthesis is not considered as an operator than the highest precedence is given to Exponent operator **
Examples for operator precedence:
>>> 200/200+(100+100)
201.0
>>>
>>> a=10
>>> b=20
>>> c=15
>>> (a+b)*(c+b)-150
900
>>> (a+b)*c+b-150
320
>>> a+b**2
410
>>> a or b + 20
10
>>> c or a + 20
15
>>> c and a + 20
30
>>> a and b + 20
40
>>> a>b>c
False
>>>
Associativity
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.