Expressions and Operators
An expression is a phrase of code that Python evaluates to produce a value. The simplest expressions are literals and identifiers. You build other expressions by joining subexpressions with the operators and/or delimiters in Table 4-2. This table lists operators in decreasing order of precedence, higher precedence before lower. Operators listed together have the same precedence. The third column lists the associativity of the operator: L (left-to-right), R (right-to-left), or NA (nonassociative).
Table 4-2. Operator precedence in expressions
Operator | Description | Associativity |
|---|---|---|
| String conversion | NA |
| Dictionary creation | NA |
| List creation | NA |
| Tuple creation or just parentheses | NA |
| Function call | L |
| Slicing | L |
| Indexing | L |
| Attribute reference | L |
| Exponentiation ( | R |
| Bitwise NOT | NA |
| Unary plus and minus | NA |
| Multiplication, division, truncating division, remainder | L |
| Addition, subtraction | L |
| Left-shift, right-shift | L |
| Bitwise AND | L |
| Bitwise XOR | L |
| Bitwise OR | L |
| Comparisons (less than, less than or equal, greater than, greater than or equal, inequality, equality)[a] | NA |
| Identity tests | NA |
| Membership tests | NA |
| Boolean NOT | NA |
| Boolean AND | L |
| Boolean OR | L |
| Anonymous simple function | NA |
[a] | ||
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access