Precedence of OperatorsΒΆ
- It is important to understand how these operators relate to the others with respect to operator precedence.
- Python will always evaluate the arithmetic operators first (** is highest, then multiplication/division, then addition/subtraction).
- Next comes the relational operators.
- Finally, the logical operators are done last.
- This means that the expression
x*5 >= 10 and y-6 <= 20will be evaluated so as to first perform the arithmetic and then check the relationships. Theandwill be done last.
The following table summarizes the operator precedence from highest to lowest. A complete table for the entire language can be found in the Python Documentation.
| Level | Category | Operators |
|---|---|---|
| 7(high) | exponent | ** |
| 6 | multiplication | *,/,//,% |
| 5 | addition | +,- |
| 4 | relational | ==,!=,<=,>=,>,< |
| 3 | logical | not |
| 2 | logical | and |
| 1(low) | logical | or |