Next / Previous / Contents / TCC Help System / NM Tech homepage

10. Expressions

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

(E)Parenthesized expression or tuple.
[E, ...]List.
{key:value, ...}Dictionary.
`...`Convert to string representation.
x.attributeAttribute reference.
x[...]Subscript or slice; see above under sequence types.
f(...)Call function f.
x**yx to the y power.
-xNegation.
~xBitwise not (one's complement).
x*yMultiplication.
x/yDivision.
x%yModulo (remainder of x/y).
x+yAddition, concatenation.
x-ySubtraction.
x<<yx shifted left ybits.
x>>yx shifted right ybits.
x&yBitwise and.
x^yBitwise exclusive or.
x|yBitwise or.
x<y, x<=y, x>y, x>=y, x!=y, x==yComparisons. These operators are all predicates (see below).
x in y, x not in yTest for membership.
x is y, x is not yTest for identity.
not xBoolean “not.”
x and yBoolean “and.”
x or yBoolean “or.”

10.1. What is a predicate?

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.