| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 1 | \chapter{Expressions and conditions} | 
 | 2 | \index{expression} | 
 | 3 | \index{condition} | 
 | 4 |  | 
 | 5 | {\bf Note:} In this and the following chapters, extended BNF notation | 
 | 6 | will be used to describe syntax, not lexical analysis. | 
 | 7 | \index{BNF} | 
 | 8 |  | 
 | 9 | This chapter explains the meaning of the elements of expressions and | 
 | 10 | conditions.  Conditions are a superset of expressions, and a condition | 
 | 11 | may be used wherever an expression is required by enclosing it in | 
 | 12 | parentheses.  The only places where expressions are used in the syntax | 
 | 13 | instead of conditions is in expression statements and on the | 
 | 14 | right-hand side of assignment statements; this catches some nasty bugs | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 15 | like accidentally writing \verb@x == 1@ instead of \verb@x = 1@. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 16 | \indexii{assignment}{statement} | 
 | 17 |  | 
 | 18 | The comma plays several roles in Python's syntax.  It is usually an | 
 | 19 | operator with a lower precedence than all others, but occasionally | 
 | 20 | serves other purposes as well; e.g. it separates function arguments, | 
 | 21 | is used in list and dictionary constructors, and has special semantics | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 22 | in \verb@print@ statements. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 23 | \index{comma} | 
 | 24 |  | 
 | 25 | When (one alternative of) a syntax rule has the form | 
 | 26 |  | 
 | 27 | \begin{verbatim} | 
 | 28 | name:           othername | 
 | 29 | \end{verbatim} | 
 | 30 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 31 | and no semantics are given, the semantics of this form of \verb@name@ | 
 | 32 | are the same as for \verb@othername@. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 33 | \index{syntax} | 
 | 34 |  | 
 | 35 | \section{Arithmetic conversions} | 
 | 36 | \indexii{arithmetic}{conversion} | 
 | 37 |  | 
 | 38 | When a description of an arithmetic operator below uses the phrase | 
 | 39 | ``the numeric arguments are converted to a common type'', | 
 | 40 | this both means that if either argument is not a number, a | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 41 | \verb@TypeError@ exception is raised, and that otherwise | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 42 | the following conversions are applied: | 
 | 43 | \exindex{TypeError} | 
 | 44 | \indexii{floating point}{number} | 
 | 45 | \indexii{long}{integer} | 
 | 46 | \indexii{plain}{integer} | 
 | 47 |  | 
 | 48 | \begin{itemize} | 
 | 49 | \item	first, if either argument is a floating point number, | 
 | 50 | 	the other is converted to floating point; | 
 | 51 | \item	else, if either argument is a long integer, | 
 | 52 | 	the other is converted to long integer; | 
 | 53 | \item	otherwise, both must be plain integers and no conversion | 
 | 54 | 	is necessary. | 
 | 55 | \end{itemize} | 
 | 56 |  | 
 | 57 | \section{Atoms} | 
 | 58 | \index{atom} | 
 | 59 |  | 
 | 60 | Atoms are the most basic elements of expressions.  Forms enclosed in | 
 | 61 | reverse quotes or in parentheses, brackets or braces are also | 
 | 62 | categorized syntactically as atoms.  The syntax for atoms is: | 
 | 63 |  | 
 | 64 | \begin{verbatim} | 
| Guido van Rossum | ad07f2b | 1995-05-03 13:47:16 +0000 | [diff] [blame] | 65 | atom:      identifier | literal | enclosure | 
 | 66 | enclosure: parenth_form|list_display|dict_display|string_conversion | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 67 | \end{verbatim} | 
 | 68 |  | 
 | 69 | \subsection{Identifiers (Names)} | 
 | 70 | \index{name} | 
 | 71 | \index{identifier} | 
 | 72 |  | 
 | 73 | An identifier occurring as an atom is a reference to a local, global | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 74 | or built-in name binding.  If a name is assigned to anywhere in a code | 
 | 75 | block (even in unreachable code), and is not mentioned in a | 
 | 76 | \verb@global@ statement in that code block, then it refers to a local | 
 | 77 | name throughout that code block.  When it is not assigned to anywhere | 
 | 78 | in the block, or when it is assigned to but also explicitly listed in | 
 | 79 | a \verb@global@ statement, it refers to a global name if one exists, | 
 | 80 | else to a built-in name (and this binding may dynamically change). | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 81 | \indexii{name}{binding} | 
 | 82 | \index{code block} | 
 | 83 | \stindex{global} | 
 | 84 | \indexii{built-in}{name} | 
 | 85 | \indexii{global}{name} | 
 | 86 |  | 
 | 87 | When the name is bound to an object, evaluation of the atom yields | 
 | 88 | that object.  When a name is not bound, an attempt to evaluate it | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 89 | raises a \verb@NameError@ exception. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 90 | \exindex{NameError} | 
 | 91 |  | 
 | 92 | \subsection{Literals} | 
 | 93 | \index{literal} | 
 | 94 |  | 
 | 95 | Python knows string and numeric literals: | 
 | 96 |  | 
 | 97 | \begin{verbatim} | 
 | 98 | literal:        stringliteral | integer | longinteger | floatnumber | 
 | 99 | \end{verbatim} | 
 | 100 |  | 
 | 101 | Evaluation of a literal yields an object of the given type (string, | 
 | 102 | integer, long integer, floating point number) with the given value. | 
 | 103 | The value may be approximated in the case of floating point literals. | 
 | 104 | See section \ref{literals} for details. | 
 | 105 |  | 
 | 106 | All literals correspond to immutable data types, and hence the | 
 | 107 | object's identity is less important than its value.  Multiple | 
 | 108 | evaluations of literals with the same value (either the same | 
 | 109 | occurrence in the program text or a different occurrence) may obtain | 
 | 110 | the same object or a different object with the same value. | 
 | 111 | \indexiii{immutable}{data}{type} | 
 | 112 |  | 
 | 113 | (In the original implementation, all literals in the same code block | 
 | 114 | with the same type and value yield the same object.) | 
 | 115 |  | 
 | 116 | \subsection{Parenthesized forms} | 
 | 117 | \index{parenthesized form} | 
 | 118 |  | 
 | 119 | A parenthesized form is an optional condition list enclosed in | 
 | 120 | parentheses: | 
 | 121 |  | 
 | 122 | \begin{verbatim} | 
 | 123 | parenth_form:      "(" [condition_list] ")" | 
 | 124 | \end{verbatim} | 
 | 125 |  | 
 | 126 | A parenthesized condition list yields whatever that condition list | 
 | 127 | yields. | 
 | 128 |  | 
 | 129 | An empty pair of parentheses yields an empty tuple object.  Since | 
 | 130 | tuples are immutable, the rules for literals apply here. | 
 | 131 | \indexii{empty}{tuple} | 
 | 132 |  | 
 | 133 | (Note that tuples are not formed by the parentheses, but rather by use | 
 | 134 | of the comma operator.  The exception is the empty tuple, for which | 
 | 135 | parentheses {\em are} required --- allowing unparenthesized ``nothing'' | 
| Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 136 | in expressions would cause ambiguities and allow common typos to | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 137 | pass uncaught.) | 
 | 138 | \index{comma} | 
 | 139 | \indexii{tuple}{display} | 
 | 140 |  | 
 | 141 | \subsection{List displays} | 
 | 142 | \indexii{list}{display} | 
 | 143 |  | 
 | 144 | A list display is a possibly empty series of conditions enclosed in | 
 | 145 | square brackets: | 
 | 146 |  | 
 | 147 | \begin{verbatim} | 
 | 148 | list_display:   "[" [condition_list] "]" | 
 | 149 | \end{verbatim} | 
 | 150 |  | 
 | 151 | A list display yields a new list object. | 
 | 152 | \obindex{list} | 
 | 153 |  | 
 | 154 | If it has no condition list, the list object has no items.  Otherwise, | 
 | 155 | the elements of the condition list are evaluated from left to right | 
 | 156 | and inserted in the list object in that order. | 
 | 157 | \indexii{empty}{list} | 
 | 158 |  | 
 | 159 | \subsection{Dictionary displays} \label{dict} | 
 | 160 | \indexii{dictionary}{display} | 
 | 161 |  | 
 | 162 | A dictionary display is a possibly empty series of key/datum pairs | 
 | 163 | enclosed in curly braces: | 
 | 164 | \index{key} | 
 | 165 | \index{datum} | 
 | 166 | \index{key/datum pair} | 
 | 167 |  | 
 | 168 | \begin{verbatim} | 
 | 169 | dict_display:   "{" [key_datum_list] "}" | 
 | 170 | key_datum_list: key_datum ("," key_datum)* [","] | 
 | 171 | key_datum:      condition ":" condition | 
 | 172 | \end{verbatim} | 
 | 173 |  | 
 | 174 | A dictionary display yields a new dictionary object. | 
 | 175 | \obindex{dictionary} | 
 | 176 |  | 
 | 177 | The key/datum pairs are evaluated from left to right to define the | 
 | 178 | entries of the dictionary: each key object is used as a key into the | 
 | 179 | dictionary to store the corresponding datum. | 
 | 180 |  | 
| Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 181 | Restrictions on the types of the key values are listed earlier in | 
 | 182 | section \ref{types}. | 
 | 183 | Clashes between duplicate keys are not detected; the last | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 184 | datum (textually rightmost in the display) stored for a given key | 
 | 185 | value prevails. | 
 | 186 | \exindex{TypeError} | 
 | 187 |  | 
 | 188 | \subsection{String conversions} | 
 | 189 | \indexii{string}{conversion} | 
| Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 190 | \indexii{reverse}{quotes} | 
 | 191 | \indexii{backward}{quotes} | 
 | 192 | \index{back-quotes} | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 193 |  | 
 | 194 | A string conversion is a condition list enclosed in reverse (or | 
 | 195 | backward) quotes: | 
 | 196 |  | 
 | 197 | \begin{verbatim} | 
 | 198 | string_conversion: "`" condition_list "`" | 
 | 199 | \end{verbatim} | 
 | 200 |  | 
 | 201 | A string conversion evaluates the contained condition list and | 
 | 202 | converts the resulting object into a string according to rules | 
 | 203 | specific to its type. | 
 | 204 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 205 | If the object is a string, a number, \verb@None@, or a tuple, list or | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 206 | dictionary containing only objects whose type is one of these, the | 
 | 207 | resulting string is a valid Python expression which can be passed to | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 208 | the built-in function \verb@eval()@ to yield an expression with the | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 209 | same value (or an approximation, if floating point numbers are | 
 | 210 | involved). | 
 | 211 |  | 
 | 212 | (In particular, converting a string adds quotes around it and converts | 
 | 213 | ``funny'' characters to escape sequences that are safe to print.) | 
 | 214 |  | 
 | 215 | It is illegal to attempt to convert recursive objects (e.g. lists or | 
 | 216 | dictionaries that contain a reference to themselves, directly or | 
 | 217 | indirectly.) | 
 | 218 | \obindex{recursive} | 
 | 219 |  | 
| Guido van Rossum | 31cce97 | 1995-01-04 19:17:34 +0000 | [diff] [blame] | 220 | The built-in function \verb@repr()@ performs exactly the same | 
 | 221 | conversion in its argument as enclosing it it reverse quotes does. | 
 | 222 | The built-in function \verb@str()@ performs a similar but more | 
 | 223 | user-friendly conversion. | 
 | 224 | \bifuncindex{repr} | 
 | 225 | \bifuncindex{str} | 
 | 226 |  | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 227 | \section{Primaries} \label{primaries} | 
 | 228 | \index{primary} | 
 | 229 |  | 
 | 230 | Primaries represent the most tightly bound operations of the language. | 
 | 231 | Their syntax is: | 
 | 232 |  | 
 | 233 | \begin{verbatim} | 
 | 234 | primary:        atom | attributeref | subscription | slicing | call | 
 | 235 | \end{verbatim} | 
 | 236 |  | 
 | 237 | \subsection{Attribute references} | 
 | 238 | \indexii{attribute}{reference} | 
 | 239 |  | 
 | 240 | An attribute reference is a primary followed by a period and a name: | 
 | 241 |  | 
 | 242 | \begin{verbatim} | 
 | 243 | attributeref:   primary "." identifier | 
 | 244 | \end{verbatim} | 
 | 245 |  | 
 | 246 | The primary must evaluate to an object of a type that supports | 
 | 247 | attribute references, e.g. a module or a list.  This object is then | 
 | 248 | asked to produce the attribute whose name is the identifier.  If this | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 249 | attribute is not available, the exception \verb@AttributeError@ is | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 250 | raised.  Otherwise, the type and value of the object produced is | 
 | 251 | determined by the object.  Multiple evaluations of the same attribute | 
 | 252 | reference may yield different objects. | 
 | 253 | \obindex{module} | 
 | 254 | \obindex{list} | 
 | 255 |  | 
 | 256 | \subsection{Subscriptions} | 
 | 257 | \index{subscription} | 
 | 258 |  | 
 | 259 | A subscription selects an item of a sequence (string, tuple or list) | 
 | 260 | or mapping (dictionary) object: | 
 | 261 | \obindex{sequence} | 
 | 262 | \obindex{mapping} | 
 | 263 | \obindex{string} | 
 | 264 | \obindex{tuple} | 
 | 265 | \obindex{list} | 
 | 266 | \obindex{dictionary} | 
 | 267 | \indexii{sequence}{item} | 
 | 268 |  | 
 | 269 | \begin{verbatim} | 
 | 270 | subscription:   primary "[" condition "]" | 
 | 271 | \end{verbatim} | 
 | 272 |  | 
 | 273 | The primary must evaluate to an object of a sequence or mapping type. | 
 | 274 |  | 
 | 275 | If it is a mapping, the condition must evaluate to an object whose | 
 | 276 | value is one of the keys of the mapping, and the subscription selects | 
 | 277 | the value in the mapping that corresponds to that key. | 
 | 278 |  | 
 | 279 | If it is a sequence, the condition must evaluate to a plain integer. | 
 | 280 | If this value is negative, the length of the sequence is added to it | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 281 | (so that, e.g. \verb@x[-1]@ selects the last item of \verb@x@.) | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 282 | The resulting value must be a nonnegative integer smaller than the | 
 | 283 | number of items in the sequence, and the subscription selects the item | 
 | 284 | whose index is that value (counting from zero). | 
 | 285 |  | 
 | 286 | A string's items are characters.  A character is not a separate data | 
 | 287 | type but a string of exactly one character. | 
 | 288 | \index{character} | 
 | 289 | \indexii{string}{item} | 
 | 290 |  | 
 | 291 | \subsection{Slicings} | 
 | 292 | \index{slicing} | 
 | 293 | \index{slice} | 
 | 294 |  | 
 | 295 | A slicing (or slice) selects a range of items in a sequence (string, | 
 | 296 | tuple or list) object: | 
 | 297 | \obindex{sequence} | 
 | 298 | \obindex{string} | 
 | 299 | \obindex{tuple} | 
 | 300 | \obindex{list} | 
 | 301 |  | 
 | 302 | \begin{verbatim} | 
 | 303 | slicing:        primary "[" [condition] ":" [condition] "]" | 
 | 304 | \end{verbatim} | 
 | 305 |  | 
 | 306 | The primary must evaluate to a sequence object.  The lower and upper | 
 | 307 | bound expressions, if present, must evaluate to plain integers; | 
 | 308 | defaults are zero and the sequence's length, respectively.  If either | 
 | 309 | bound is negative, the sequence's length is added to it.  The slicing | 
| Guido van Rossum | a547547 | 1995-03-16 14:44:07 +0000 | [diff] [blame] | 310 | now selects all items with index \var{k} such that | 
 | 311 | \code{\var{i} <= \var{k} < \var{j}} where \var{i} | 
 | 312 | and \var{j} are the specified lower and upper bounds.  This may be an | 
 | 313 | empty sequence.  It is not an error if \var{i} or \var{j} lie outside the | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 314 | range of valid indexes (such items don't exist so they aren't | 
 | 315 | selected). | 
 | 316 |  | 
 | 317 | \subsection{Calls} \label{calls} | 
 | 318 | \index{call} | 
 | 319 |  | 
 | 320 | A call calls a callable object (e.g. a function) with a possibly empty | 
| Guido van Rossum | 8476d00 | 1995-10-08 01:07:06 +0000 | [diff] [blame] | 321 | series of arguments:\footnote{The new syntax for keyword arguments is | 
 | 322 | not yet documented in this manual.  See chapter 12 of the Tutorial.} | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 323 | \obindex{callable} | 
 | 324 |  | 
 | 325 | \begin{verbatim} | 
 | 326 | call:           primary "(" [condition_list] ")" | 
 | 327 | \end{verbatim} | 
 | 328 |  | 
 | 329 | The primary must evaluate to a callable object (user-defined | 
 | 330 | functions, built-in functions, methods of built-in objects, class | 
 | 331 | objects, and methods of class instances are callable).  If it is a | 
 | 332 | class, the argument list must be empty; otherwise, the arguments are | 
 | 333 | evaluated. | 
 | 334 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 335 | A call always returns some value, possibly \verb@None@, unless it | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 336 | raises an exception.  How this value is computed depends on the type | 
 | 337 | of the callable object.  If it is: | 
 | 338 |  | 
 | 339 | \begin{description} | 
 | 340 |  | 
 | 341 | \item[a user-defined function:] the code block for the function is | 
 | 342 | executed, passing it the argument list.  The first thing the code | 
 | 343 | block will do is bind the formal parameters to the arguments; this is | 
 | 344 | described in section \ref{function}.  When the code block executes a | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 345 | \verb@return@ statement, this specifies the return value of the | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 346 | function call. | 
 | 347 | \indexii{function}{call} | 
 | 348 | \indexiii{user-defined}{function}{call} | 
 | 349 | \obindex{user-defined function} | 
 | 350 | \obindex{function} | 
 | 351 |  | 
 | 352 | \item[a built-in function or method:] the result is up to the | 
 | 353 | interpreter; see the library reference manual for the descriptions of | 
 | 354 | built-in functions and methods. | 
 | 355 | \indexii{function}{call} | 
 | 356 | \indexii{built-in function}{call} | 
 | 357 | \indexii{method}{call} | 
 | 358 | \indexii{built-in method}{call} | 
 | 359 | \obindex{built-in method} | 
 | 360 | \obindex{built-in function} | 
 | 361 | \obindex{method} | 
 | 362 | \obindex{function} | 
 | 363 |  | 
 | 364 | \item[a class object:] a new instance of that class is returned. | 
 | 365 | \obindex{class} | 
 | 366 | \indexii{class object}{call} | 
 | 367 |  | 
 | 368 | \item[a class instance method:] the corresponding user-defined | 
 | 369 | function is called, with an argument list that is one longer than the | 
 | 370 | argument list of the call: the instance becomes the first argument. | 
 | 371 | \obindex{class instance} | 
 | 372 | \obindex{instance} | 
 | 373 | \indexii{instance}{call} | 
 | 374 | \indexii{class instance}{call} | 
 | 375 |  | 
 | 376 | \end{description} | 
 | 377 |  | 
 | 378 | \section{Unary arithmetic operations} | 
 | 379 | \indexiii{unary}{arithmetic}{operation} | 
 | 380 | \indexiii{unary}{bit-wise}{operation} | 
 | 381 |  | 
 | 382 | All unary arithmetic (and bit-wise) operations have the same priority: | 
 | 383 |  | 
 | 384 | \begin{verbatim} | 
 | 385 | u_expr:         primary | "-" u_expr | "+" u_expr | "~" u_expr | 
 | 386 | \end{verbatim} | 
 | 387 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 388 | The unary \verb@"-"@ (minus) operator yields the negation of its | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 389 | numeric argument. | 
 | 390 | \index{negation} | 
 | 391 | \index{minus} | 
 | 392 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 393 | The unary \verb@"+"@ (plus) operator yields its numeric argument | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 394 | unchanged. | 
 | 395 | \index{plus} | 
 | 396 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 397 | The unary \verb@"~"@ (invert) operator yields the bit-wise inversion | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 398 | of its plain or long integer argument.  The bit-wise inversion of | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 399 | \verb@x@ is defined as \verb@-(x+1)@. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 400 | \index{inversion} | 
 | 401 |  | 
 | 402 | In all three cases, if the argument does not have the proper type, | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 403 | a \verb@TypeError@ exception is raised. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 404 | \exindex{TypeError} | 
 | 405 |  | 
 | 406 | \section{Binary arithmetic operations} | 
 | 407 | \indexiii{binary}{arithmetic}{operation} | 
 | 408 |  | 
 | 409 | The binary arithmetic operations have the conventional priority | 
 | 410 | levels.  Note that some of these operations also apply to certain | 
 | 411 | non-numeric types.  There is no ``power'' operator, so there are only | 
 | 412 | two levels, one for multiplicative operators and one for additive | 
 | 413 | operators: | 
 | 414 |  | 
 | 415 | \begin{verbatim} | 
 | 416 | m_expr:         u_expr | m_expr "*" u_expr | 
 | 417 |               | m_expr "/" u_expr | m_expr "%" u_expr | 
 | 418 | a_expr:         m_expr | aexpr "+" m_expr | aexpr "-" m_expr | 
 | 419 | \end{verbatim} | 
 | 420 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 421 | The \verb@"*"@ (multiplication) operator yields the product of its | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 422 | arguments.  The arguments must either both be numbers, or one argument | 
 | 423 | must be a plain integer and the other must be a sequence.  In the | 
 | 424 | former case, the numbers are converted to a common type and then | 
 | 425 | multiplied together.  In the latter case, sequence repetition is | 
 | 426 | performed; a negative repetition factor yields an empty sequence. | 
 | 427 | \index{multiplication} | 
 | 428 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 429 | The \verb@"/"@ (division) operator yields the quotient of its | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 430 | arguments.  The numeric arguments are first converted to a common | 
 | 431 | type.  Plain or long integer division yields an integer of the same | 
 | 432 | type; the result is that of mathematical division with the `floor' | 
 | 433 | function applied to the result.  Division by zero raises the | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 434 | \verb@ZeroDivisionError@ exception. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 435 | \exindex{ZeroDivisionError} | 
 | 436 | \index{division} | 
 | 437 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 438 | The \verb@"%"@ (modulo) operator yields the remainder from the | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 439 | division of the first argument by the second.  The numeric arguments | 
 | 440 | are first converted to a common type.  A zero right argument raises | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 441 | the \verb@ZeroDivisionError@ exception.  The arguments may be floating | 
 | 442 | point numbers, e.g. \verb@3.14 % 0.7@ equals \verb@0.34@.  The modulo | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 443 | operator always yields a result with the same sign as its second | 
 | 444 | operand (or zero); the absolute value of the result is strictly | 
 | 445 | smaller than the second operand. | 
 | 446 | \index{modulo} | 
 | 447 |  | 
 | 448 | The integer division and modulo operators are connected by the | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 449 | following identity: \verb@x == (x/y)*y + (x%y)@.  Integer division and | 
 | 450 | modulo are also connected with the built-in function \verb@divmod()@: | 
 | 451 | \verb@divmod(x, y) == (x/y, x%y)@.  These identities don't hold for | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 452 | floating point numbers; there a similar identity holds where | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 453 | \verb@x/y@ is replaced by \verb@floor(x/y)@). | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 454 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 455 | The \verb@"+"@ (addition) operator yields the sum of its arguments. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 456 | The arguments must either both be numbers, or both sequences of the | 
 | 457 | same type.  In the former case, the numbers are converted to a common | 
 | 458 | type and then added together.  In the latter case, the sequences are | 
 | 459 | concatenated. | 
 | 460 | \index{addition} | 
 | 461 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 462 | The \verb@"-"@ (subtraction) operator yields the difference of its | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 463 | arguments.  The numeric arguments are first converted to a common | 
 | 464 | type. | 
 | 465 | \index{subtraction} | 
 | 466 |  | 
 | 467 | \section{Shifting operations} | 
 | 468 | \indexii{shifting}{operation} | 
 | 469 |  | 
 | 470 | The shifting operations have lower priority than the arithmetic | 
 | 471 | operations: | 
 | 472 |  | 
 | 473 | \begin{verbatim} | 
 | 474 | shift_expr:     a_expr | shift_expr ( "<<" | ">>" ) a_expr | 
 | 475 | \end{verbatim} | 
 | 476 |  | 
 | 477 | These operators accept plain or long integers as arguments.  The | 
 | 478 | arguments are converted to a common type.  They shift the first | 
 | 479 | argument to the left or right by the number of bits given by the | 
 | 480 | second argument. | 
 | 481 |  | 
| Guido van Rossum | a547547 | 1995-03-16 14:44:07 +0000 | [diff] [blame] | 482 | A right shift by \var{n} bits is defined as division by | 
 | 483 | \code{pow(2,\var{n})}.  A left shift by \var{n} bits is defined as | 
 | 484 | multiplication with \code{pow(2,\var{n})}; for plain integers there is | 
 | 485 | no overflow check so this drops bits and flips the sign if the result | 
 | 486 | is not less than \code{pow(2,31)} in absolute value. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 487 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 488 | Negative shift counts raise a \verb@ValueError@ exception. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 489 | \exindex{ValueError} | 
 | 490 |  | 
 | 491 | \section{Binary bit-wise operations} | 
 | 492 | \indexiii{binary}{bit-wise}{operation} | 
 | 493 |  | 
 | 494 | Each of the three bitwise operations has a different priority level: | 
 | 495 |  | 
 | 496 | \begin{verbatim} | 
 | 497 | and_expr:       shift_expr | and_expr "&" shift_expr | 
 | 498 | xor_expr:       and_expr | xor_expr "^" and_expr | 
 | 499 | or_expr:       xor_expr | or_expr "|" xor_expr | 
 | 500 | \end{verbatim} | 
 | 501 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 502 | The \verb@"&"@ operator yields the bitwise AND of its arguments, which | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 503 | must be plain or long integers.  The arguments are converted to a | 
 | 504 | common type. | 
 | 505 | \indexii{bit-wise}{and} | 
 | 506 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 507 | The \verb@"^"@ operator yields the bitwise XOR (exclusive OR) of its | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 508 | arguments, which must be plain or long integers.  The arguments are | 
 | 509 | converted to a common type. | 
 | 510 | \indexii{bit-wise}{xor} | 
 | 511 | \indexii{exclusive}{or} | 
 | 512 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 513 | The \verb@"|"@ operator yields the bitwise (inclusive) OR of its | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 514 | arguments, which must be plain or long integers.  The arguments are | 
 | 515 | converted to a common type. | 
 | 516 | \indexii{bit-wise}{or} | 
 | 517 | \indexii{inclusive}{or} | 
 | 518 |  | 
 | 519 | \section{Comparisons} | 
 | 520 | \index{comparison} | 
 | 521 |  | 
 | 522 | Contrary to C, all comparison operations in Python have the same | 
 | 523 | priority, which is lower than that of any arithmetic, shifting or | 
 | 524 | bitwise operation.  Also contrary to C, expressions like | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 525 | \verb@a < b < c@ have the interpretation that is conventional in | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 526 | mathematics: | 
 | 527 | \index{C} | 
 | 528 |  | 
 | 529 | \begin{verbatim} | 
 | 530 | comparison:     or_expr (comp_operator or_expr)* | 
 | 531 | comp_operator:  "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in" | 
 | 532 | \end{verbatim} | 
 | 533 |  | 
 | 534 | Comparisons yield integer values: 1 for true, 0 for false. | 
 | 535 |  | 
| Guido van Rossum | a547547 | 1995-03-16 14:44:07 +0000 | [diff] [blame] | 536 | Comparisons can be chained arbitrarily, e.g. \code{x < y <= z} is | 
 | 537 | equivalent to \code{x < y and y <= z}, except that \code{y} is | 
 | 538 | evaluated only once (but in both cases \code{z} is not evaluated at all | 
 | 539 | when \code{x < y} is found to be false). | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 540 | \indexii{chaining}{comparisons} | 
 | 541 |  | 
| Guido van Rossum | a547547 | 1995-03-16 14:44:07 +0000 | [diff] [blame] | 542 | Formally, if \var{a}, \var{b}, \var{c}, \ldots, \var{y}, \var{z} are | 
 | 543 | expressions and \var{opa}, \var{opb}, \ldots, \var{opy} are comparison | 
 | 544 | operators, then \var{a opa b opb c} \ldots \var{y opy z} is equivalent | 
 | 545 | to \var{a opa b} \code{and} \var{b opb c} \code{and} \ldots \code{and} | 
 | 546 | \var{y opy z}, except that each expression is evaluated at most once. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 547 |  | 
| Guido van Rossum | a547547 | 1995-03-16 14:44:07 +0000 | [diff] [blame] | 548 | Note that \var{a opa b opb c} doesn't imply any kind of comparison | 
 | 549 | between \var{a} and \var{c}, so that e.g.\ \code{x < y > z} is | 
 | 550 | perfectly legal (though perhaps not pretty). | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 551 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 552 | The forms \verb@<>@ and \verb@!=@ are equivalent; for consistency with | 
 | 553 | C, \verb@!=@ is preferred; where \verb@!=@ is mentioned below | 
 | 554 | \verb@<>@ is also implied. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 555 |  | 
 | 556 | The operators {\tt "<", ">", "==", ">=", "<="}, and {\tt "!="} compare | 
 | 557 | the values of two objects.  The objects needn't have the same type. | 
 | 558 | If both are numbers, they are coverted to a common type.  Otherwise, | 
 | 559 | objects of different types {\em always} compare unequal, and are | 
 | 560 | ordered consistently but arbitrarily. | 
 | 561 |  | 
 | 562 | (This unusual definition of comparison is done to simplify the | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 563 | definition of operations like sorting and the \verb@in@ and | 
| Guido van Rossum | a547547 | 1995-03-16 14:44:07 +0000 | [diff] [blame] | 564 | \verb@not@ \verb@in@ operators.) | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 565 |  | 
 | 566 | Comparison of objects of the same type depends on the type: | 
 | 567 |  | 
 | 568 | \begin{itemize} | 
 | 569 |  | 
 | 570 | \item | 
 | 571 | Numbers are compared arithmetically. | 
 | 572 |  | 
 | 573 | \item | 
 | 574 | Strings are compared lexicographically using the numeric equivalents | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 575 | (the result of the built-in function \verb@ord@) of their characters. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 576 |  | 
 | 577 | \item | 
 | 578 | Tuples and lists are compared lexicographically using comparison of | 
 | 579 | corresponding items. | 
 | 580 |  | 
 | 581 | \item | 
 | 582 | Mappings (dictionaries) are compared through lexicographic | 
 | 583 | comparison of their sorted (key, value) lists.% | 
 | 584 | \footnote{This is expensive since it requires sorting the keys first, | 
| Guido van Rossum | b2c6556 | 1993-05-12 08:53:36 +0000 | [diff] [blame] | 585 | but about the only sensible definition.  An earlier version of Python | 
 | 586 | compared dictionaries by identity only, but this caused surprises | 
 | 587 | because people expected to be able to test a dictionary for emptiness | 
 | 588 | by comparing it to {\tt \{\}}.} | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 589 |  | 
 | 590 | \item | 
 | 591 | Most other types compare unequal unless they are the same object; | 
 | 592 | the choice whether one object is considered smaller or larger than | 
 | 593 | another one is made arbitrarily but consistently within one | 
 | 594 | execution of a program. | 
 | 595 |  | 
 | 596 | \end{itemize} | 
 | 597 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 598 | The operators \verb@in@ and \verb@not in@ test for sequence | 
| Guido van Rossum | a547547 | 1995-03-16 14:44:07 +0000 | [diff] [blame] | 599 | membership: if \var{y} is a sequence, \code{\var{x} in \var{y}} is | 
 | 600 | true if and only if there exists an index \var{i} such that | 
 | 601 | \code{\var{x} = \var{y}[\var{i}]}. | 
 | 602 | \code{\var{x} not in \var{y}} yields the inverse truth value.  The | 
 | 603 | exception \verb@TypeError@ is raised when \var{y} is not a sequence, | 
 | 604 | or when \var{y} is a string and \var{x} is not a string of length one.% | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 605 | \footnote{The latter restriction is sometimes a nuisance.} | 
 | 606 | \opindex{in} | 
 | 607 | \opindex{not in} | 
 | 608 | \indexii{membership}{test} | 
 | 609 | \obindex{sequence} | 
 | 610 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 611 | The operators \verb@is@ and \verb@is not@ test for object identity: | 
| Guido van Rossum | a547547 | 1995-03-16 14:44:07 +0000 | [diff] [blame] | 612 | \var{x} \code{is} \var{y} is true if and only if \var{x} and \var{y} | 
 | 613 | are the same object.  \var{x} \code{is not} \var{y} yields the inverse | 
 | 614 | truth value. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 615 | \opindex{is} | 
 | 616 | \opindex{is not} | 
 | 617 | \indexii{identity}{test} | 
 | 618 |  | 
 | 619 | \section{Boolean operations} \label{Booleans} | 
 | 620 | \indexii{Boolean}{operation} | 
 | 621 |  | 
 | 622 | Boolean operations have the lowest priority of all Python operations: | 
 | 623 |  | 
 | 624 | \begin{verbatim} | 
| Guido van Rossum | 3cbc16d | 1993-12-17 12:13:53 +0000 | [diff] [blame] | 625 | condition:      or_test | lambda_form | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 626 | or_test:        and_test | or_test "or" and_test | 
 | 627 | and_test:       not_test | and_test "and" not_test | 
 | 628 | not_test:       comparison | "not" not_test | 
| Guido van Rossum | 3cbc16d | 1993-12-17 12:13:53 +0000 | [diff] [blame] | 629 | lambda_form:	"lambda" [parameter_list]: condition | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 630 | \end{verbatim} | 
 | 631 |  | 
 | 632 | In the context of Boolean operations, and also when conditions are | 
 | 633 | used by control flow statements, the following values are interpreted | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 634 | as false: \verb@None@, numeric zero of all types, empty sequences | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 635 | (strings, tuples and lists), and empty mappings (dictionaries).  All | 
 | 636 | other values are interpreted as true. | 
 | 637 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 638 | The operator \verb@not@ yields 1 if its argument is false, 0 otherwise. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 639 | \opindex{not} | 
 | 640 |  | 
| Guido van Rossum | a547547 | 1995-03-16 14:44:07 +0000 | [diff] [blame] | 641 | The condition \var{x} \verb@and@ \var{y} first evaluates \var{x}; if | 
 | 642 | \var{x} is false, its value is returned; otherwise, \var{y} is | 
 | 643 | evaluated and the resulting value is returned. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 644 | \opindex{and} | 
 | 645 |  | 
| Guido van Rossum | a547547 | 1995-03-16 14:44:07 +0000 | [diff] [blame] | 646 | The condition \var{x} \verb@or@ \var{y} first evaluates \var{x}; if | 
 | 647 | \var{x} is true, its value is returned; otherwise, \var{y} is | 
 | 648 | evaluated and the resulting value is returned. | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 649 | \opindex{or} | 
 | 650 |  | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 651 | (Note that \verb@and@ and \verb@or@ do not restrict the value and type | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 652 | they return to 0 and 1, but rather return the last evaluated argument. | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 653 | This is sometimes useful, e.g. if \verb@s@ is a string that should be | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 654 | replaced by a default value if it is empty, the expression | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 655 | \verb@s or 'foo'@ yields the desired value.  Because \verb@not@ has to | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 656 | invent a value anyway, it does not bother to return a value of the | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 657 | same type as its argument, so e.g. \verb@not 'foo'@ yields \verb@0@, | 
 | 658 | not \verb@''@.) | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 659 |  | 
| Guido van Rossum | 3cbc16d | 1993-12-17 12:13:53 +0000 | [diff] [blame] | 660 | Lambda forms (lambda expressions) have the same syntactic position as | 
 | 661 | conditions.  They are a shorthand to create anonymous functions; the | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 662 | expression {\em {\tt lambda} arguments{\tt :} condition} | 
| Guido van Rossum | 3cbc16d | 1993-12-17 12:13:53 +0000 | [diff] [blame] | 663 | yields a function object that behaves virtually identical to one | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 664 | defined with | 
 | 665 | {\em {\tt def} name {\tt (}arguments{\tt ): return} condition}. | 
 | 666 | See section \ref{function} for the syntax of | 
| Guido van Rossum | 3cbc16d | 1993-12-17 12:13:53 +0000 | [diff] [blame] | 667 | parameter lists.  Note that functions created with lambda forms cannot | 
 | 668 | contain statements. | 
 | 669 | \label{lambda} | 
 | 670 | \indexii{lambda}{expression} | 
 | 671 | \indexii{lambda}{form} | 
 | 672 | \indexii{anonmymous}{function} | 
 | 673 |  | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 674 | \section{Expression lists and condition lists} | 
 | 675 | \indexii{expression}{list} | 
 | 676 | \indexii{condition}{list} | 
 | 677 |  | 
 | 678 | \begin{verbatim} | 
 | 679 | expr_list:      or_expr ("," or_expr)* [","] | 
 | 680 | cond_list:      condition ("," condition)* [","] | 
 | 681 | \end{verbatim} | 
 | 682 |  | 
 | 683 | The only difference between expression lists and condition lists is | 
 | 684 | the lowest priority of operators that can be used in them without | 
 | 685 | being enclosed in parentheses; condition lists allow all operators, | 
 | 686 | while expression lists don't allow comparisons and Boolean operators | 
 | 687 | (they do allow bitwise and shift operators though). | 
 | 688 |  | 
 | 689 | Expression lists are used in expression statements and assignments; | 
 | 690 | condition lists are used everywhere else where a list of | 
 | 691 | comma-separated values is required. | 
 | 692 |  | 
 | 693 | An expression (condition) list containing at least one comma yields a | 
 | 694 | tuple.  The length of the tuple is the number of expressions | 
 | 695 | (conditions) in the list.  The expressions (conditions) are evaluated | 
| Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 696 | from left to right.  (Condition lists are used syntactically is a few | 
| Guido van Rossum | 46f3e00 | 1992-08-14 09:11:01 +0000 | [diff] [blame] | 697 | places where no tuple is constructed but a list of values is needed | 
 | 698 | nevertheless.) | 
 | 699 | \obindex{tuple} | 
 | 700 |  | 
 | 701 | The trailing comma is required only to create a single tuple (a.k.a. a | 
 | 702 | {\em singleton}); it is optional in all other cases.  A single | 
 | 703 | expression (condition) without a trailing comma doesn't create a | 
 | 704 | tuple, but rather yields the value of that expression (condition). | 
 | 705 | \indexii{trailing}{comma} | 
 | 706 |  | 
 | 707 | (To create an empty tuple, use an empty pair of parentheses: | 
| Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 708 | \verb@()@.) | 
| Guido van Rossum | 4dd6b84 | 1995-03-17 15:59:52 +0000 | [diff] [blame] | 709 |  | 
 | 710 | \section{Summary} | 
 | 711 |  | 
 | 712 | The following table summarizes the operator precedences in Python, | 
 | 713 | from lowest precedence (least binding) to highest precedence (most | 
 | 714 | binding).  Operators in the same box have the same precedence.  Unless | 
 | 715 | the syntax is explicitly given, operators are binary.  Operators in | 
 | 716 | the same box group left to right (except for comparisons, which | 
 | 717 | chain from left to right --- see above). | 
 | 718 |  | 
 | 719 | \begin{center} | 
 | 720 | \begin{tabular}{|c|c|} | 
 | 721 | \hline | 
 | 722 | \code{or} & Boolean OR \\ | 
 | 723 | \hline | 
 | 724 | \code{and} & Boolean AND \\ | 
 | 725 | \hline | 
 | 726 | \code{not} \var{x} & Boolean NOT \\ | 
 | 727 | \hline | 
 | 728 | \code{in}, \code{not} \code{in} & Membership tests \\ | 
 | 729 | \code{is}, \code{is} \code{not} & Identity tests \\ | 
 | 730 | \code{<}, \code{<=}, \code{>}, \code{>=}, \code{<>}, \code{!=}, \code{=} & | 
 | 731 | 	Comparisons \\ | 
 | 732 | \hline | 
 | 733 | \code{|} & Bitwise OR \\ | 
 | 734 | \hline | 
 | 735 | \code{\^} & Bitwise XOR \\ | 
 | 736 | \hline | 
 | 737 | \code{\&} & Bitwise AND \\ | 
 | 738 | \hline | 
 | 739 | \code{<<}, \code{>>} & Shifts \\ | 
 | 740 | \hline | 
 | 741 | \code{+}, \code{-} & Addition and subtraction \\ | 
 | 742 | \hline | 
 | 743 | \code{*}, \code{/}, \code{\%} & Multiplication, division, remainder \\ | 
 | 744 | \hline | 
 | 745 | \code{+\var{x}}, \code{-\var{x}} & Positive, negative \\ | 
 | 746 | \code{\~\var{x}} & Bitwise not \\ | 
 | 747 | \hline | 
 | 748 | \code{\var{x}.\var{attribute}} & Attribute reference \\ | 
 | 749 | \code{\var{x}[\var{index}]} &  Subscription \\ | 
 | 750 | \code{\var{x}[\var{index}:\var{index}]} &  Slicing \\ | 
 | 751 | \code{\var{f}(\var{arguments}...)} & Function call \\ | 
 | 752 | \hline | 
 | 753 | \code{(\var{expressions}\ldots)} & Binding or tuple display \\ | 
 | 754 | \code{[\var{expressions}\ldots]} & List display \\ | 
 | 755 | \code{\{\var{key}:\var{datum}\ldots\}} & Dictionary display \\ | 
 | 756 | \code{`\var{expression}\ldots`} & String conversion \\ | 
 | 757 | \hline | 
 | 758 | \end{tabular} | 
 | 759 | \end{center} |