Python's operators are shown here from highest precedence to lowest, with a ruled line separating groups of operators with equal precedence:
Table 1. Python 2.2 operator precedences
( | Parenthesized expression or tuple. |
[ | List. |
{ | Dictionary. |
`...` | Convert to string representation. |
| Attribute reference. |
| Subscript or slice; see above under sequence types. |
| Call function f. |
| x to the
y power. |
- | Negation. |
~ | Bitwise not (one's complement). |
| Multiplication. |
| Division. |
| Modulo (remainder of x/y). |
| Addition, concatenation. |
| Subtraction. |
| x shifted left
ybits. |
| x shifted right
ybits. |
| Bitwise and. |
| Bitwise exclusive or. |
| Bitwise or. |
,
,
,
,
,
| Comparisons. These operators are all predicates (see below). |
,
| Test for membership. |
,
| Test for identity. |
not | Boolean “not.” |
| Boolean “and.” |
| Boolean “or.” |
We use the term predicate
to mean any Python function that tests some condition
and returns a true or false value. For example,
x < y is a predicate that
tests whether x is less than
y. Predicates generally return
1 for true and 0 for false. For example,
5 < 500 returns 1, while
5 >= 500 returns 0.