Rules of precedence used by python to evaluate an expression
Python calculates expressions based on the below rules , these are generally the same as those in algebra.
- Expression in Parenthesis
- Parenthesis ( ) are evaluated first , any expression between the parenthesis will evaluate the value first and results will be further processed.
- Exponentiation
- are the second set of expression that are evaluated, if there are multiple expression then Python will evaluate each from right to left.
- Multiplication , Division and Modulus
- are on the same level of precedence. In case of multiple occurrence Python will evaluate each from left to right.
- Addition and Substraction
- are on the same level of precedence. these are evaluated last unless covered in parenthesis.In case of multiple occurrence these are evaluated from left to right.
Complete list of precedence is available at Python.org