Next: Simple statements Up: Expressions and conditions Previous: Boolean operations

Expression lists and condition lists


expr_list:      or_expr ("," or_expr)* [","]
cond_list:      condition ("," condition)* [","]

The only difference between expression lists and condition lists is the lowest priority of operators that can be used in them without being enclosed in parentheses; condition lists allow all operators, while expression lists don't allow comparisons and Boolean operators (they do allow bitwise and shift operators though).

Expression lists are used in expression statements and assignments; condition lists are used everywhere else where a list of comma-separated values is required.

An expression (condition) list containing at least one comma yields a tuple. The length of the tuple is the number of expressions (conditions) in the list. The expressions (conditions) are evaluated from left to right. (Conditions lists are used syntactically is a few places where no tuple is constructed but a list of values is needed nevertheless.)

The trailing comma is required only to create a single tuple (a.k.a. a singleton); it is optional in all other cases. A single expression (condition) without a trailing comma doesn't create a tuple, but rather yields the value of that expression (condition).

(To create an empty tuple, use an empty pair of parentheses: ().)


guido@cwi.nl