## 4.4. Expressions and OperatorsAn 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).
In Table 4-2, indicate any expression, while y and attr indicate any identifier. The notation arg,... means commas join zero or more repetitions, except for string conversion, where you need one or more repetitions. A trailing comma is allowed and innocuous in all such cases, except for string conversion, where it's forbidden. The string conversion operator, with its quirky behavior, is not recommended; use built-in function repr (covered in repr on page 166) instead.## 4.4.1. Comparison ChainingYou can chain comparisons, implying a logical 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 once at the most. ## 4.4.2. Short-Circuiting OperatorsOperators In other words, . If x is false, the result is x; otherwise, the result is x. Similarly, yxor first evaluates y. If x is true, the result is x; otherwise, the result is x.y
## 4.4.2.1. The Python 2.5 ternary operatorPython 2.5 introduces another short-circuiting operator, the ternary operator
Each of is an arbitrary expression. condition evaluates first. If condition is true, the result is condition; otherwise, the result is whentrue. Only one of the two subexpressions whenfalse and whentrue evaluates, depending on the truth value of whenfalse.conditionThe order of the three subexpressions in this new ternary operator may be a bit confusing. Also, a recommended style is to always place parentheses around the whole expression. |