[ Team LiB ] Previous Section Next Section

4.4 Expressions and Operators

An expression is a phrase of code that the Python interpreter can evaluate 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 the operators in decreasing order of precedence, so operators with higher precedence are listed before those with lower precedence. Operators listed together have the same precedence. The A column lists the associativity of the operator, which can be L (left-to-right), R (right-to-left), or NA (non-associative).

In Table 4-2, expr, key, f, index, x, and y indicate any expression, while attr and arg indicate identifiers. The notation ,... indicates that commas join zero or more repetitions, except for string conversion, where one or more repetitions are allowed. A trailing comma is also allowed and innocuous in all such cases, except with string conversion, where it's forbidden.

Table 4-2. Operator precedence in expressions

Operator

Description

A

`expr,...`

String conversion

NA

{key:expr,...}

Dictionary creation

NA

[expr,...]

List creation

NA

(expr,...)

Tuple creation or simple parentheses

NA

f(expr,...)

Function call

L

x[index:index]

Slicing

L

x[index]

Indexing

L

x.attr

Attribute reference

L

x**y

Exponentiation (x to yth power)

R

~x

Bitwise NOT

NA

+x, -x

Unary plus and minus

NA

x*y, x/y, x//y, x%y

Multiplication, division, truncating division, remainder

L

x+y, x-y

Addition, subtraction

L

x<<y, x>>y

Left-shift, right-shift

L

x&y

Bitwise AND

L

x^y

Bitwise XOR

L

x|y

Bitwise OR

L

x<y, x<=y, x>y, x>=y, x<>y, x!=y, x= =y

Comparisons (less than, less than or equal, greater than, greater than or equal, inequality, equality)[2]

NA

x is y, x is not y

Identity tests

NA

x in y, x not in y

Membership tests

NA

not x

Boolean NOT

NA

x and y

Boolean AND

L

x or y

Boolean OR

L

lambda arg,...: expr

Anonymous simple function

NA

[2] Note that <> and != are alternate forms of the same operator, where != is the preferred version and <> is obsolete.

You can chain comparisons, implying a logical and. For example:

a < b <= c < d

has the same meaning as:

a < b and b <= c and c < d

The chained form is more readable and evaluates each subexpression only once.

Operators and and or short-circuit their operands' evaluation: the right-hand operand evaluates only if its value is needed to get the truth value of the entire and or or operation. In other words, x and y first evaluates x and if x is false, the result is x; otherwise, the result is y. By the same token, x or y first evaluates x and if x is true, the result is x; otherwise, the result is y. Note that and and or don't force their results to be True or False, but rather return one or the other of their operands. This lets you use these operators more generally, not just in Boolean contexts. and and or, because of their short-circuiting semantics, differ from all other operators, which fully evaluate all operands before performing the operation. As such, and and or let the left operand act as a guard for the right operand.

    [ Team LiB ] Previous Section Next Section