What is the order of precedence in python

Order of precedence is the set of rules about which operations should be performed first and the in what order they may be operated.
Python follows the same order of precedence as of mathematics.
- Parentheses have the highest precedence. The expressions inside parentheses are evaluated first.
- Example: The result of
2 + ( 3 - 1)
is 4. First 3-1 is evaluated and its result is added with 2, thus the output being 4.
- Example: The result of
- Exponentiation has the next highest precedence.
- Example: The expression
3**2+1
gives the result 10 and not 9.
- Example: The expression
- Multiplication and division operators have the same precedence but less than exponentiation.
- Example: The result of the expression
4*2+1
is 9 , not 12.
- Example: The result of the expression
- Addition and subtraction operators have the same precedence but less than multiplication and division.
- Example: The expression
6/2-1
gives 2, not 6
- Example: The expression
- Operators with the same precedence are evaluated from left-to-right.
- Example: The result of the operation
5+4/2*3
is 11.
- Example: The result of the operation
Subscribe
Login
Please login to comment
0 Discussion