Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 1 | \chapter{Expressions\label{expressions}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 2 | \index{expression} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 4 | This chapter explains the meaning of the elements of expressions in |
| 5 | Python. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 7 | \strong{Syntax Notes:} In this and the following chapters, extended |
| 8 | BNF\index{BNF} notation will be used to describe syntax, not lexical |
| 9 | analysis. When (one alternative of) a syntax rule has the form |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 10 | |
| 11 | \begin{verbatim} |
| 12 | name: othername |
| 13 | \end{verbatim} |
| 14 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 15 | and no semantics are given, the semantics of this form of \code{name} |
| 16 | are the same as for \code{othername}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 17 | \index{syntax} |
| 18 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 19 | \section{Arithmetic conversions\label{conversions}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 20 | \indexii{arithmetic}{conversion} |
| 21 | |
| 22 | When a description of an arithmetic operator below uses the phrase |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 23 | ``the numeric arguments are converted to a common type,'' the |
| 24 | arguments are coerced using the coercion rules listed at the end of |
| 25 | chapter 3. If both arguments are standard numeric types, the |
| 26 | following coercions are applied: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 27 | |
| 28 | \begin{itemize} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 29 | \item If either argument is a complex number, the other is converted |
| 30 | to complex; |
| 31 | \item otherwise, if either argument is a floating point number, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 32 | the other is converted to floating point; |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 33 | \item otherwise, if either argument is a long integer, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 34 | the other is converted to long integer; |
| 35 | \item otherwise, both must be plain integers and no conversion |
| 36 | is necessary. |
| 37 | \end{itemize} |
| 38 | |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 39 | Some additional rules apply for certain operators (e.g., a string left |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 40 | argument to the `\%' operator). Extensions can define their own |
| 41 | coercions. |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | \section{Atoms\label{atoms}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 45 | \index{atom} |
| 46 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 47 | Atoms are the most basic elements of expressions. The simplest atoms |
| 48 | are identifiers or literals. Forms enclosed in |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 49 | reverse quotes or in parentheses, brackets or braces are also |
| 50 | categorized syntactically as atoms. The syntax for atoms is: |
| 51 | |
| 52 | \begin{verbatim} |
| 53 | atom: identifier | literal | enclosure |
| 54 | enclosure: parenth_form|list_display|dict_display|string_conversion |
| 55 | \end{verbatim} |
| 56 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 57 | \subsection{Identifiers (Names)\label{atom-identifiers}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 58 | \index{name} |
| 59 | \index{identifier} |
| 60 | |
| 61 | An identifier occurring as an atom is a reference to a local, global |
| 62 | or built-in name binding. If a name is assigned to anywhere in a code |
| 63 | block (even in unreachable code), and is not mentioned in a |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 64 | \keyword{global} statement in that code block, then it refers to a local |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 65 | name throughout that code block. When it is not assigned to anywhere |
| 66 | in the block, or when it is assigned to but also explicitly listed in |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 67 | a \keyword{global} statement, it refers to a global name if one exists, |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 68 | else to a built-in name (and this binding may dynamically change). |
| 69 | \indexii{name}{binding} |
| 70 | \index{code block} |
| 71 | \stindex{global} |
| 72 | \indexii{built-in}{name} |
| 73 | \indexii{global}{name} |
| 74 | |
| 75 | When the name is bound to an object, evaluation of the atom yields |
| 76 | that object. When a name is not bound, an attempt to evaluate it |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 77 | raises a \exception{NameError} exception. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 78 | \exindex{NameError} |
| 79 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 80 | \strong{Private name mangling:}% |
| 81 | \indexii{name}{mangling}% |
| 82 | \indexii{private}{names}% |
| 83 | when an identifier that textually occurs in a class definition begins |
| 84 | with two or more underscore characters and does not end in two or more |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 85 | underscores, it is considered a \dfn{private name} of that class. |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 86 | Private names are transformed to a longer form before code is |
| 87 | generated for them. The transformation inserts the class name in |
| 88 | front of the name, with leading underscores removed, and a single |
| 89 | underscore inserted in front of the class name. For example, the |
| 90 | identifier \code{__spam} occurring in a class named \code{Ham} will be |
| 91 | transformed to \code{_Ham__spam}. This transformation is independent |
| 92 | of the syntactical context in which the identifier is used. If the |
| 93 | transformed name is extremely long (longer than 255 characters), |
| 94 | implementation defined truncation may happen. If the class name |
| 95 | consists only of underscores, no transformation is done. |
| 96 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 97 | \subsection{Literals\label{atom-literals}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 98 | \index{literal} |
| 99 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 100 | Python supports string literals and various numeric literals: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 101 | |
| 102 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 103 | literal: stringliteral | integer | longinteger | floatnumber | imagnumber |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 104 | \end{verbatim} |
| 105 | |
| 106 | Evaluation of a literal yields an object of the given type (string, |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 107 | integer, long integer, floating point number, complex number) with the |
| 108 | given value. The value may be approximated in the case of floating |
| 109 | point and imaginary (complex) literals. See section \ref{literals} |
| 110 | for details. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 111 | |
| 112 | All literals correspond to immutable data types, and hence the |
| 113 | object's identity is less important than its value. Multiple |
| 114 | evaluations of literals with the same value (either the same |
| 115 | occurrence in the program text or a different occurrence) may obtain |
| 116 | the same object or a different object with the same value. |
| 117 | \indexiii{immutable}{data}{type} |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 118 | \indexii{immutable}{object} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 119 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 120 | \subsection{Parenthesized forms\label{parenthesized}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 121 | \index{parenthesized form} |
| 122 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 123 | A parenthesized form is an optional expression list enclosed in |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 124 | parentheses: |
| 125 | |
| 126 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 127 | parenth_form: "(" [expression_list] ")" |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 128 | \end{verbatim} |
| 129 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 130 | A parenthesized expression list yields whatever that expression list |
| 131 | yields: if the list contains at least one comma, it yields a tuple; |
| 132 | otherwise, it yields the single expression that makes up the |
| 133 | expression list. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 134 | |
| 135 | An empty pair of parentheses yields an empty tuple object. Since |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 136 | tuples are immutable, the rules for literals apply (i.e., two |
| 137 | occurrences of the empty tuple may or may not yield the same object). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 138 | \indexii{empty}{tuple} |
| 139 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 140 | Note that tuples are not formed by the parentheses, but rather by use |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 141 | of the comma operator. The exception is the empty tuple, for which |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 142 | parentheses \emph{are} required --- allowing unparenthesized ``nothing'' |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 143 | in expressions would cause ambiguities and allow common typos to |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 144 | pass uncaught. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 145 | \index{comma} |
| 146 | \indexii{tuple}{display} |
| 147 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 148 | \subsection{List displays\label{lists}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 149 | \indexii{list}{display} |
| 150 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 151 | A list display is a possibly empty series of expressions enclosed in |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 152 | square brackets: |
| 153 | |
| 154 | \begin{verbatim} |
Fred Drake | a1e214a | 2000-08-15 17:54:49 +0000 | [diff] [blame^] | 155 | list_display: "[" [listmaker] "]" |
| 156 | listmaker: expression_list ( list_iter | ( "," expression)* [","] ) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 157 | list_iter: list_for | list_if |
| 158 | list_for: "for" expression_list "in" testlist [list_iter] |
| 159 | list_if: "if" test [list_iter] |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 160 | \end{verbatim} |
| 161 | |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 162 | A list display yields a new list object. Its contents are specified |
| 163 | by providing either a list of expressions or a list comprehension. |
| 164 | When a comma-separated list of expressions is supplied, its elements are |
| 165 | evaluated from left to right and placed into the list object in that |
| 166 | order. When a list comprehension is supplied, it consists of a |
| 167 | single expression followed by one or more "for" or "if" clauses. In this |
| 168 | case, the elements of the new list are those that would be produced |
| 169 | by considering each of the "for" or "if" clauses a block, nesting from |
| 170 | left to right, and evaluating the expression to produce a list element |
| 171 | each time the innermost block is reached. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 172 | \obindex{list} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 173 | \indexii{empty}{list} |
| 174 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 175 | \subsection{Dictionary displays\label{dict}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 176 | \indexii{dictionary}{display} |
| 177 | |
| 178 | A dictionary display is a possibly empty series of key/datum pairs |
| 179 | enclosed in curly braces: |
| 180 | \index{key} |
| 181 | \index{datum} |
| 182 | \index{key/datum pair} |
| 183 | |
| 184 | \begin{verbatim} |
| 185 | dict_display: "{" [key_datum_list] "}" |
| 186 | key_datum_list: key_datum ("," key_datum)* [","] |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 187 | key_datum: expression ":" expression |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 188 | \end{verbatim} |
| 189 | |
| 190 | A dictionary display yields a new dictionary object. |
| 191 | \obindex{dictionary} |
| 192 | |
| 193 | The key/datum pairs are evaluated from left to right to define the |
| 194 | entries of the dictionary: each key object is used as a key into the |
| 195 | dictionary to store the corresponding datum. |
| 196 | |
| 197 | Restrictions on the types of the key values are listed earlier in |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 198 | section \ref{types}. (To summarize,the key type should be hashable, |
| 199 | which excludes all mutable objects.) Clashes between duplicate keys |
| 200 | are not detected; the last datum (textually rightmost in the display) |
| 201 | stored for a given key value prevails. |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 202 | \indexii{immutable}{object} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 203 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 204 | \subsection{String conversions\label{string-conversions}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 205 | \indexii{string}{conversion} |
| 206 | \indexii{reverse}{quotes} |
| 207 | \indexii{backward}{quotes} |
| 208 | \index{back-quotes} |
| 209 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 210 | A string conversion is an expression list enclosed in reverse (a.k.a. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 211 | backward) quotes: |
| 212 | |
| 213 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 214 | string_conversion: "`" expression_list "`" |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 215 | \end{verbatim} |
| 216 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 217 | A string conversion evaluates the contained expression list and |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 218 | converts the resulting object into a string according to rules |
| 219 | specific to its type. |
| 220 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 221 | If the object is a string, a number, \code{None}, or a tuple, list or |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 222 | dictionary containing only objects whose type is one of these, the |
| 223 | resulting string is a valid Python expression which can be passed to |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 224 | the built-in function \function{eval()} to yield an expression with the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 225 | same value (or an approximation, if floating point numbers are |
| 226 | involved). |
| 227 | |
| 228 | (In particular, converting a string adds quotes around it and converts |
| 229 | ``funny'' characters to escape sequences that are safe to print.) |
| 230 | |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 231 | It is illegal to attempt to convert recursive objects (e.g., lists or |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 232 | dictionaries that contain a reference to themselves, directly or |
| 233 | indirectly.) |
| 234 | \obindex{recursive} |
| 235 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 236 | The built-in function \function{repr()} performs exactly the same |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 237 | conversion in its argument as enclosing it in parentheses and reverse |
| 238 | quotes does. The built-in function \function{str()} performs a |
| 239 | similar but more user-friendly conversion. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 240 | \bifuncindex{repr} |
| 241 | \bifuncindex{str} |
| 242 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 243 | \section{Primaries\label{primaries}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 244 | \index{primary} |
| 245 | |
| 246 | Primaries represent the most tightly bound operations of the language. |
| 247 | Their syntax is: |
| 248 | |
| 249 | \begin{verbatim} |
| 250 | primary: atom | attributeref | subscription | slicing | call |
| 251 | \end{verbatim} |
| 252 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 253 | \subsection{Attribute references\label{attribute-references}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 254 | \indexii{attribute}{reference} |
| 255 | |
| 256 | An attribute reference is a primary followed by a period and a name: |
| 257 | |
| 258 | \begin{verbatim} |
| 259 | attributeref: primary "." identifier |
| 260 | \end{verbatim} |
| 261 | |
| 262 | The primary must evaluate to an object of a type that supports |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 263 | attribute references, e.g., a module or a list. This object is then |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 264 | asked to produce the attribute whose name is the identifier. If this |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 265 | attribute is not available, the exception |
| 266 | \exception{AttributeError}\exindex{AttributeError} is raised. |
| 267 | Otherwise, the type and value of the object produced is determined by |
| 268 | the object. Multiple evaluations of the same attribute reference may |
| 269 | yield different objects. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 270 | \obindex{module} |
| 271 | \obindex{list} |
| 272 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 273 | \subsection{Subscriptions\label{subscriptions}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 274 | \index{subscription} |
| 275 | |
| 276 | A subscription selects an item of a sequence (string, tuple or list) |
| 277 | or mapping (dictionary) object: |
| 278 | \obindex{sequence} |
| 279 | \obindex{mapping} |
| 280 | \obindex{string} |
| 281 | \obindex{tuple} |
| 282 | \obindex{list} |
| 283 | \obindex{dictionary} |
| 284 | \indexii{sequence}{item} |
| 285 | |
| 286 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 287 | subscription: primary "[" expression_list "]" |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 288 | \end{verbatim} |
| 289 | |
| 290 | The primary must evaluate to an object of a sequence or mapping type. |
| 291 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 292 | If the primary is a mapping, the expression list must evaluate to an |
| 293 | object whose value is one of the keys of the mapping, and the |
| 294 | subscription selects the value in the mapping that corresponds to that |
| 295 | key. (The expression list is a tuple except if it has exactly one |
| 296 | item.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 297 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 298 | If the primary is a sequence, the expression (list) must evaluate to a |
| 299 | plain integer. If this value is negative, the length of the sequence |
| 300 | is added to it (so that, e.g., \code{x[-1]} selects the last item of |
| 301 | \code{x}.) The resulting value must be a nonnegative integer less |
| 302 | than the number of items in the sequence, and the subscription selects |
| 303 | the item whose index is that value (counting from zero). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 304 | |
| 305 | A string's items are characters. A character is not a separate data |
| 306 | type but a string of exactly one character. |
| 307 | \index{character} |
| 308 | \indexii{string}{item} |
| 309 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 310 | \subsection{Slicings\label{slicings}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 311 | \index{slicing} |
| 312 | \index{slice} |
| 313 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 314 | A slicing selects a range of items in a sequence object (e.g., a |
| 315 | string, tuple or list). Slicings may be used as expressions or as |
| 316 | targets in assignment or del statements. The syntax for a slicing: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 317 | \obindex{sequence} |
| 318 | \obindex{string} |
| 319 | \obindex{tuple} |
| 320 | \obindex{list} |
| 321 | |
| 322 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 323 | slicing: simple_slicing | extended_slicing |
| 324 | simple_slicing: primary "[" short_slice "]" |
| 325 | extended_slicing: primary "[" slice_list "]" |
| 326 | slice_list: slice_item ("," slice_item)* [","] |
| 327 | slice_item: expression | proper_slice | ellipsis |
| 328 | proper_slice: short_slice | long_slice |
| 329 | short_slice: [lower_bound] ":" [upper_bound] |
| 330 | long_slice: short_slice ":" [stride] |
| 331 | lower_bound: expression |
| 332 | upper_bound: expression |
| 333 | stride: expression |
| 334 | ellipsis: "..." |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 335 | \end{verbatim} |
| 336 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 337 | There is ambiguity in the formal syntax here: anything that looks like |
| 338 | an expression list also looks like a slice list, so any subscription |
| 339 | can be interpreted as a slicing. Rather than further complicating the |
| 340 | syntax, this is disambiguated by defining that in this case the |
| 341 | interpretation as a subscription takes priority over the |
| 342 | interpretation as a slicing (this is the case if the slice list |
| 343 | contains no proper slice nor ellipses). Similarly, when the slice |
| 344 | list has exactly one short slice and no trailing comma, the |
| 345 | interpretation as a simple slicing takes priority over that as an |
| 346 | extended slicing.\indexii{extended}{slicing} |
| 347 | |
| 348 | The semantics for a simple slicing are as follows. The primary must |
| 349 | evaluate to a sequence object. The lower and upper bound expressions, |
| 350 | if present, must evaluate to plain integers; defaults are zero and the |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 351 | \code{sys.maxint}, respectively. If either bound is negative, the |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 352 | sequence's length is added to it. The slicing now selects all items |
| 353 | with index \var{k} such that |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 354 | \code{\var{i} <= \var{k} < \var{j}} where \var{i} |
| 355 | and \var{j} are the specified lower and upper bounds. This may be an |
| 356 | empty sequence. It is not an error if \var{i} or \var{j} lie outside the |
| 357 | range of valid indexes (such items don't exist so they aren't |
| 358 | selected). |
| 359 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 360 | The semantics for an extended slicing are as follows. The primary |
| 361 | must evaluate to a mapping object, and it is indexed with a key that |
| 362 | is constructed from the slice list, as follows. If the slice list |
| 363 | contains at least one comma, the key is a tuple containing the |
| 364 | conversion of the slice items; otherwise, the conversion of the lone |
| 365 | slice item is the key. The conversion of a slice item that is an |
| 366 | expression is that expression. The conversion of an ellipsis slice |
| 367 | item is the built-in \code{Ellipsis} object. The conversion of a |
| 368 | proper slice is a slice object (see section \ref{types}) whose |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 369 | \member{start}, \member{stop} and \member{step} attributes are the |
| 370 | values of the expressions given as lower bound, upper bound and |
| 371 | stride, respectively, substituting \code{None} for missing |
| 372 | expressions. |
Fred Drake | 99cd573 | 1999-02-12 20:40:09 +0000 | [diff] [blame] | 373 | \withsubitem{(slice object attribute)}{\ttindex{start} |
| 374 | \ttindex{stop}\ttindex{step}} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 375 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 376 | \subsection{Calls\label{calls}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 377 | \index{call} |
| 378 | |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 379 | A call calls a callable object (e.g., a function) with a possibly empty |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 380 | series of arguments: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 381 | \obindex{callable} |
| 382 | |
| 383 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 384 | call: primary "(" [argument_list [","]] ")" |
| 385 | argument_list: positional_arguments ["," keyword_arguments] |
| 386 | | keyword_arguments |
| 387 | positional_arguments: expression ("," expression)* |
| 388 | keyword_arguments: keyword_item ("," keyword_item)* |
| 389 | keyword_item: identifier "=" expression |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 390 | \end{verbatim} |
| 391 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 392 | A trailing comma may be present after an argument list but does not |
| 393 | affect the semantics. |
| 394 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 395 | The primary must evaluate to a callable object (user-defined |
| 396 | functions, built-in functions, methods of built-in objects, class |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 397 | objects, methods of class instances, and certain class instances |
| 398 | themselves are callable; extensions may define additional callable |
| 399 | object types). All argument expressions are evaluated before the call |
| 400 | is attempted. Please refer to section \ref{function} for the syntax |
| 401 | of formal parameter lists. |
| 402 | |
| 403 | If keyword arguments are present, they are first converted to |
| 404 | positional arguments, as follows. First, a list of unfilled slots is |
| 405 | created for the formal parameters. If there are N positional |
| 406 | arguments, they are placed in the first N slots. Next, for each |
| 407 | keyword argument, the identifier is used to determine the |
| 408 | corresponding slot (if the identifier is the same as the first formal |
| 409 | parameter name, the first slot is used, and so on). If the slot is |
| 410 | already filled, a \exception{TypeError} exception is raised. |
| 411 | Otherwise, the value of the argument is placed in the slot, filling it |
| 412 | (even if the expression is \code{None}, it fills the slot). When all |
| 413 | arguments have been processed, the slots that are still unfilled are |
| 414 | filled with the corresponding default value from the function |
| 415 | definition. (Default values are calculated, once, when the function |
| 416 | is defined; thus, a mutable object such as a list or dictionary used |
| 417 | as default value will be shared by all calls that don't specify an |
| 418 | argument value for the corresponding slot; this should usually be |
| 419 | avoided.) If there are any unfilled slots for which no default value |
| 420 | is specified, a \exception{TypeError} exception is raised. Otherwise, |
| 421 | the list of filled slots is used as the argument list for the call. |
| 422 | |
| 423 | If there are more positional arguments than there are formal parameter |
| 424 | slots, a \exception{TypeError} exception is raised, unless a formal |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 425 | parameter using the syntax \samp{*identifier} is present; in this |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 426 | case, that formal parameter receives a tuple containing the excess |
| 427 | positional arguments (or an empty tuple if there were no excess |
| 428 | positional arguments). |
| 429 | |
| 430 | If any keyword argument does not correspond to a formal parameter |
| 431 | name, a \exception{TypeError} exception is raised, unless a formal |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 432 | parameter using the syntax \samp{**identifier} is present; in this |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 433 | case, that formal parameter receives a dictionary containing the |
| 434 | excess keyword arguments (using the keywords as keys and the argument |
| 435 | values as corresponding values), or a (new) empty dictionary if there |
| 436 | were no excess keyword arguments. |
| 437 | |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 438 | Formal parameters using the syntax \samp{*identifier} or |
| 439 | \samp{**identifier} cannot be used as positional argument slots or |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 440 | as keyword argument names. Formal parameters using the syntax |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 441 | \samp{(sublist)} cannot be used as keyword argument names; the |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 442 | outermost sublist corresponds to a single unnamed argument slot, and |
| 443 | the argument value is assigned to the sublist using the usual tuple |
| 444 | assignment rules after all other parameter processing is done. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 445 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 446 | A call always returns some value, possibly \code{None}, unless it |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 447 | raises an exception. How this value is computed depends on the type |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 448 | of the callable object. |
| 449 | |
| 450 | If it is--- |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 451 | |
| 452 | \begin{description} |
| 453 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 454 | \item[a user-defined function:] The code block for the function is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 455 | executed, passing it the argument list. The first thing the code |
| 456 | block will do is bind the formal parameters to the arguments; this is |
| 457 | described in section \ref{function}. When the code block executes a |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 458 | \keyword{return} statement, this specifies the return value of the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 459 | function call. |
| 460 | \indexii{function}{call} |
| 461 | \indexiii{user-defined}{function}{call} |
| 462 | \obindex{user-defined function} |
| 463 | \obindex{function} |
| 464 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 465 | \item[a built-in function or method:] The result is up to the |
Fred Drake | 3d83fc3 | 2000-07-31 20:08:23 +0000 | [diff] [blame] | 466 | interpreter; see the \citetitle[../lib/built-in-funcs.html]{Python |
| 467 | Library Reference} for the descriptions of built-in functions and |
| 468 | methods. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 469 | \indexii{function}{call} |
| 470 | \indexii{built-in function}{call} |
| 471 | \indexii{method}{call} |
| 472 | \indexii{built-in method}{call} |
| 473 | \obindex{built-in method} |
| 474 | \obindex{built-in function} |
| 475 | \obindex{method} |
| 476 | \obindex{function} |
| 477 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 478 | \item[a class object:] A new instance of that class is returned. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 479 | \obindex{class} |
| 480 | \indexii{class object}{call} |
| 481 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 482 | \item[a class instance method:] The corresponding user-defined |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 483 | function is called, with an argument list that is one longer than the |
| 484 | argument list of the call: the instance becomes the first argument. |
| 485 | \obindex{class instance} |
| 486 | \obindex{instance} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 487 | \indexii{class instance}{call} |
| 488 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 489 | \item[a class instance:] The class must define a \method{__call__()} |
| 490 | method; the effect is then the same as if that method was called. |
| 491 | \indexii{instance}{call} |
Fred Drake | ea81edf | 1998-11-25 17:51:15 +0000 | [diff] [blame] | 492 | \withsubitem{(object method)}{\ttindex{__call__()}} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 493 | |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 494 | \end{description} |
| 495 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 496 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 497 | \section{The power operator\label{power}} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 498 | |
| 499 | The power operator binds more tightly than unary operators on its |
| 500 | left; it binds less tightly than unary operators on its right. The |
| 501 | syntax is: |
| 502 | |
| 503 | \begin{verbatim} |
| 504 | power: primary ["**" u_expr] |
| 505 | \end{verbatim} |
| 506 | |
| 507 | Thus, in an unparenthesized sequence of power and unary operators, the |
| 508 | operators are evaluated from right to left (this does not constrain |
| 509 | the evaluation order for the operands). |
| 510 | |
| 511 | The power operator has the same semantics as the built-in |
| 512 | \function{pow()} function, when called with two arguments: it yields |
| 513 | its left argument raised to the power of its right argument. The |
| 514 | numeric arguments are first converted to a common type. The result |
| 515 | type is that of the arguments after coercion; if the result is not |
| 516 | expressible in that type (as in raising an integer to a negative |
| 517 | power, or a negative floating point number to a broken power), a |
| 518 | \exception{TypeError} exception is raised. |
| 519 | |
| 520 | |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 521 | \section{Unary arithmetic operations \label{unary}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 522 | \indexiii{unary}{arithmetic}{operation} |
| 523 | \indexiii{unary}{bit-wise}{operation} |
| 524 | |
| 525 | All unary arithmetic (and bit-wise) operations have the same priority: |
| 526 | |
| 527 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 528 | u_expr: power | "-" u_expr | "+" u_expr | "~" u_expr |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 529 | \end{verbatim} |
| 530 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 531 | The unary \code{-} (minus) operator yields the negation of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 532 | numeric argument. |
| 533 | \index{negation} |
| 534 | \index{minus} |
| 535 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 536 | The unary \code{+} (plus) operator yields its numeric argument |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 537 | unchanged. |
| 538 | \index{plus} |
| 539 | |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 540 | The unary \code{\~} (invert) operator yields the bit-wise inversion |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 541 | of its plain or long integer argument. The bit-wise inversion of |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 542 | \code{x} is defined as \code{-(x+1)}. It only applies to integral |
| 543 | numbers. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 544 | \index{inversion} |
| 545 | |
| 546 | In all three cases, if the argument does not have the proper type, |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 547 | a \exception{TypeError} exception is raised. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 548 | \exindex{TypeError} |
| 549 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 550 | \section{Binary arithmetic operations\label{binary}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 551 | \indexiii{binary}{arithmetic}{operation} |
| 552 | |
| 553 | The binary arithmetic operations have the conventional priority |
| 554 | levels. Note that some of these operations also apply to certain |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 555 | non-numeric types. Apart from the power operator, there are only two |
| 556 | levels, one for multiplicative operators and one for additive |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 557 | operators: |
| 558 | |
| 559 | \begin{verbatim} |
| 560 | m_expr: u_expr | m_expr "*" u_expr |
| 561 | | m_expr "/" u_expr | m_expr "%" u_expr |
| 562 | a_expr: m_expr | aexpr "+" m_expr | aexpr "-" m_expr |
| 563 | \end{verbatim} |
| 564 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 565 | The \code{*} (multiplication) operator yields the product of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 566 | arguments. The arguments must either both be numbers, or one argument |
| 567 | must be a plain integer and the other must be a sequence. In the |
| 568 | former case, the numbers are converted to a common type and then |
| 569 | multiplied together. In the latter case, sequence repetition is |
| 570 | performed; a negative repetition factor yields an empty sequence. |
| 571 | \index{multiplication} |
| 572 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 573 | The \code{/} (division) operator yields the quotient of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 574 | arguments. The numeric arguments are first converted to a common |
| 575 | type. Plain or long integer division yields an integer of the same |
| 576 | type; the result is that of mathematical division with the `floor' |
| 577 | function applied to the result. Division by zero raises the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 578 | \exception{ZeroDivisionError} exception. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 579 | \exindex{ZeroDivisionError} |
| 580 | \index{division} |
| 581 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 582 | The \code{\%} (modulo) operator yields the remainder from the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 583 | division of the first argument by the second. The numeric arguments |
| 584 | are first converted to a common type. A zero right argument raises |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 585 | the \exception{ZeroDivisionError} exception. The arguments may be floating |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 586 | point numbers, e.g., \code{3.14\%0.7} equals \code{0.34} (since |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 587 | \code{3.14} equals \code{4*0.7 + 0.34}.) The modulo operator always |
| 588 | yields a result with the same sign as its second operand (or zero); |
| 589 | the absolute value of the result is strictly smaller than the second |
| 590 | operand. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 591 | \index{modulo} |
| 592 | |
| 593 | The integer division and modulo operators are connected by the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 594 | following identity: \code{x == (x/y)*y + (x\%y)}. Integer division and |
| 595 | modulo are also connected with the built-in function \function{divmod()}: |
| 596 | \code{divmod(x, y) == (x/y, x\%y)}. These identities don't hold for |
Fred Drake | 1ea7c75 | 1999-05-06 14:46:35 +0000 | [diff] [blame] | 597 | floating point and complex numbers; there similar identities hold |
| 598 | approximately where \code{x/y} is replaced by \code{floor(x/y)}) or |
| 599 | \code{floor(x/y) - 1} (for floats),\footnote{ |
| 600 | If x is very close to an exact integer multiple of y, it's |
| 601 | possible for \code{floor(x/y)} to be one larger than |
| 602 | \code{(x-x\%y)/y} due to rounding. In such cases, Python returns |
| 603 | the latter result, in order to preserve that \code{divmod(x,y)[0] |
| 604 | * y + x \%{} y} be very close to \code{x}. |
| 605 | } or \code{floor((x/y).real)} (for |
| 606 | complex). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 607 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 608 | The \code{+} (addition) operator yields the sum of its arguments. |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 609 | The arguments must either both be numbers or both sequences of the |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 610 | same type. In the former case, the numbers are converted to a common |
| 611 | type and then added together. In the latter case, the sequences are |
| 612 | concatenated. |
| 613 | \index{addition} |
| 614 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 615 | The \code{-} (subtraction) operator yields the difference of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 616 | arguments. The numeric arguments are first converted to a common |
| 617 | type. |
| 618 | \index{subtraction} |
| 619 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 620 | \section{Shifting operations\label{shifting}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 621 | \indexii{shifting}{operation} |
| 622 | |
| 623 | The shifting operations have lower priority than the arithmetic |
| 624 | operations: |
| 625 | |
| 626 | \begin{verbatim} |
| 627 | shift_expr: a_expr | shift_expr ( "<<" | ">>" ) a_expr |
| 628 | \end{verbatim} |
| 629 | |
| 630 | These operators accept plain or long integers as arguments. The |
| 631 | arguments are converted to a common type. They shift the first |
| 632 | argument to the left or right by the number of bits given by the |
| 633 | second argument. |
| 634 | |
| 635 | A right shift by \var{n} bits is defined as division by |
| 636 | \code{pow(2,\var{n})}. A left shift by \var{n} bits is defined as |
| 637 | multiplication with \code{pow(2,\var{n})}; for plain integers there is |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 638 | no overflow check so in that case the operation drops bits and flips |
| 639 | the sign if the result is not less than \code{pow(2,31)} in absolute |
| 640 | value. Negative shift counts raise a \exception{ValueError} |
| 641 | exception. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 642 | \exindex{ValueError} |
| 643 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 644 | \section{Binary bit-wise operations\label{bitwise}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 645 | \indexiii{binary}{bit-wise}{operation} |
| 646 | |
| 647 | Each of the three bitwise operations has a different priority level: |
| 648 | |
| 649 | \begin{verbatim} |
| 650 | and_expr: shift_expr | and_expr "&" shift_expr |
| 651 | xor_expr: and_expr | xor_expr "^" and_expr |
| 652 | or_expr: xor_expr | or_expr "|" xor_expr |
| 653 | \end{verbatim} |
| 654 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 655 | The \code{\&} operator yields the bitwise AND of its arguments, which |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 656 | must be plain or long integers. The arguments are converted to a |
| 657 | common type. |
| 658 | \indexii{bit-wise}{and} |
| 659 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 660 | The \code{\^} operator yields the bitwise XOR (exclusive OR) of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 661 | arguments, which must be plain or long integers. The arguments are |
| 662 | converted to a common type. |
| 663 | \indexii{bit-wise}{xor} |
| 664 | \indexii{exclusive}{or} |
| 665 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 666 | The \code{|} operator yields the bitwise (inclusive) OR of its |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 667 | arguments, which must be plain or long integers. The arguments are |
| 668 | converted to a common type. |
| 669 | \indexii{bit-wise}{or} |
| 670 | \indexii{inclusive}{or} |
| 671 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 672 | \section{Comparisons\label{comparisons}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 673 | \index{comparison} |
| 674 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 675 | Contrary to \C, all comparison operations in Python have the same |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 676 | priority, which is lower than that of any arithmetic, shifting or |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 677 | bitwise operation. Also contrary to \C, expressions like |
| 678 | \code{a < b < c} have the interpretation that is conventional in |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 679 | mathematics: |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 680 | \indexii{C}{language} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 681 | |
| 682 | \begin{verbatim} |
| 683 | comparison: or_expr (comp_operator or_expr)* |
| 684 | comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in" |
| 685 | \end{verbatim} |
| 686 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 687 | Comparisons yield integer values: \code{1} for true, \code{0} for false. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 688 | |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 689 | Comparisons can be chained arbitrarily, e.g., \code{x < y <= z} is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 690 | equivalent to \code{x < y and y <= z}, except that \code{y} is |
| 691 | evaluated only once (but in both cases \code{z} is not evaluated at all |
| 692 | when \code{x < y} is found to be false). |
| 693 | \indexii{chaining}{comparisons} |
| 694 | |
| 695 | Formally, if \var{a}, \var{b}, \var{c}, \ldots, \var{y}, \var{z} are |
| 696 | expressions and \var{opa}, \var{opb}, \ldots, \var{opy} are comparison |
| 697 | operators, then \var{a opa b opb c} \ldots \var{y opy z} is equivalent |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 698 | to \var{a opa b} \keyword{and} \var{b opb c} \keyword{and} \ldots |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 699 | \var{y opy z}, except that each expression is evaluated at most once. |
| 700 | |
| 701 | Note that \var{a opa b opb c} doesn't imply any kind of comparison |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 702 | between \var{a} and \var{c}, so that, e.g., \code{x < y > z} is |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 703 | perfectly legal (though perhaps not pretty). |
| 704 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 705 | The forms \code{<>} and \code{!=} are equivalent; for consistency with |
| 706 | C, \code{!=} is preferred; where \code{!=} is mentioned below |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 707 | \code{<>} is also acceptable. At some point in the (far) future, |
| 708 | \code{<>} may become obsolete. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 709 | |
Fred Drake | d03268f | 1998-11-25 19:23:33 +0000 | [diff] [blame] | 710 | The operators \texttt{"<", ">", "==", ">=", "<="}, and \texttt{"!="} compare |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 711 | the values of two objects. The objects needn't have the same type. |
| 712 | If both are numbers, they are coverted to a common type. Otherwise, |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 713 | objects of different types \emph{always} compare unequal, and are |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 714 | ordered consistently but arbitrarily. |
| 715 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 716 | (This unusual definition of comparison was used to simplify the |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 717 | definition of operations like sorting and the \keyword{in} and |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 718 | \keyword{not in} operators. In the future, the comparison rules for |
| 719 | objects of different types are likely to change.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 720 | |
| 721 | Comparison of objects of the same type depends on the type: |
| 722 | |
| 723 | \begin{itemize} |
| 724 | |
| 725 | \item |
| 726 | Numbers are compared arithmetically. |
| 727 | |
| 728 | \item |
| 729 | Strings are compared lexicographically using the numeric equivalents |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 730 | (the result of the built-in function \function{ord()}) of their |
| 731 | characters. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 732 | |
| 733 | \item |
| 734 | Tuples and lists are compared lexicographically using comparison of |
| 735 | corresponding items. |
| 736 | |
| 737 | \item |
| 738 | Mappings (dictionaries) are compared through lexicographic |
Fred Drake | b55ce1e | 1999-04-05 21:32:52 +0000 | [diff] [blame] | 739 | comparison of their sorted (key, value) lists.\footnote{ |
| 740 | This is expensive since it requires sorting the keys first, |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 741 | but it is about the only sensible definition. An earlier version of |
| 742 | Python compared dictionaries by identity only, but this caused |
| 743 | surprises because people expected to be able to test a dictionary for |
| 744 | emptiness by comparing it to \code{\{\}}.} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 745 | |
| 746 | \item |
| 747 | Most other types compare unequal unless they are the same object; |
| 748 | the choice whether one object is considered smaller or larger than |
| 749 | another one is made arbitrarily but consistently within one |
| 750 | execution of a program. |
| 751 | |
| 752 | \end{itemize} |
| 753 | |
Fred Drake | 7399b9e | 2000-07-11 19:43:47 +0000 | [diff] [blame] | 754 | The operators \keyword{in} and \keyword{not in} test for set |
| 755 | membership: every type can define membership in whatever way is |
| 756 | appropriate. Traditionally, this interface has been tightly bound |
| 757 | the sequence interface, which is related in that presence in a sequence |
| 758 | can be usefully interpreted as membership in a set. |
| 759 | |
| 760 | For the list, tuple types, \code{\var{x} in \var{y}} is true if and only |
| 761 | if there exists such an index \var{i} such that |
| 762 | \code{var{x} == \var{y}[\var{i}]} is true. |
| 763 | |
| 764 | For the Unicode and string types, \code{\var{x} in \var{y}} is true if and only |
| 765 | if there exists such an index \var{i} such that |
| 766 | \code{var{x} == \var{y}[\var{i}]} is true. If \code{\var{x}} is not |
| 767 | a string of length \code{1} or a unicode object of length \code{1}, |
| 768 | a \exception{TypeError} exception is raised. |
| 769 | |
| 770 | For user-defined classes which define the \method{__contains__()} method, |
| 771 | \code{\var{x} in \var{y}} is true if and only if |
| 772 | \code{\var{y}.__contains__(\var{x})} is true. |
| 773 | |
| 774 | For user-defined classes which do not define \method{__contains__()} and |
| 775 | do define \var{__getitem__}, \code{\var{x} in \var{y}} is true if and only |
| 776 | if there is a non-negative integer index \var{i} such that |
| 777 | \code{\var{x} == \var{y}[\var{i}]}, and all lower integer indices |
| 778 | do not raise \exception{IndexError} exception. (If any other exception |
| 779 | is raised, it is as if \keyword{in} raised that exception). |
| 780 | |
| 781 | The operator \keyword{not in} is defined to have the inverse true value |
| 782 | of \keyword{in}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 783 | \opindex{in} |
| 784 | \opindex{not in} |
| 785 | \indexii{membership}{test} |
| 786 | \obindex{sequence} |
| 787 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 788 | The operators \keyword{is} and \keyword{is not} test for object identity: |
| 789 | \code{\var{x} is \var{y}} is true if and only if \var{x} and \var{y} |
| 790 | are the same object. \code{\var{x} is not \var{y}} yields the inverse |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 791 | truth value. |
| 792 | \opindex{is} |
| 793 | \opindex{is not} |
| 794 | \indexii{identity}{test} |
| 795 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 796 | \section{Boolean operations\label{Booleans}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 797 | \indexii{Boolean}{operation} |
| 798 | |
| 799 | Boolean operations have the lowest priority of all Python operations: |
| 800 | |
| 801 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 802 | expression: or_test | lambda_form |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 803 | or_test: and_test | or_test "or" and_test |
| 804 | and_test: not_test | and_test "and" not_test |
| 805 | not_test: comparison | "not" not_test |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 806 | lambda_form: "lambda" [parameter_list]: expression |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 807 | \end{verbatim} |
| 808 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 809 | In the context of Boolean operations, and also when expressions are |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 810 | used by control flow statements, the following values are interpreted |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 811 | as false: \code{None}, numeric zero of all types, empty sequences |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 812 | (strings, tuples and lists), and empty mappings (dictionaries). All |
| 813 | other values are interpreted as true. |
| 814 | |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 815 | The operator \keyword{not} yields \code{1} if its argument is false, |
| 816 | \code{0} otherwise. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 817 | \opindex{not} |
| 818 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 819 | The expression \code{\var{x} and \var{y}} first evaluates \var{x}; if |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 820 | \var{x} is false, its value is returned; otherwise, \var{y} is |
| 821 | evaluated and the resulting value is returned. |
| 822 | \opindex{and} |
| 823 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 824 | The expression \code{\var{x} or \var{y}} first evaluates \var{x}; if |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 825 | \var{x} is true, its value is returned; otherwise, \var{y} is |
| 826 | evaluated and the resulting value is returned. |
| 827 | \opindex{or} |
| 828 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 829 | (Note that neither \keyword{and} nor \keyword{or} restrict the value |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 830 | and type they return to \code{0} and \code{1}, but rather return the |
| 831 | last evaluated argument. |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 832 | This is sometimes useful, e.g., if \code{s} is a string that should be |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 833 | replaced by a default value if it is empty, the expression |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 834 | \code{s or 'foo'} yields the desired value. Because \keyword{not} has to |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 835 | invent a value anyway, it does not bother to return a value of the |
Guido van Rossum | 7c0240f | 1998-07-24 15:36:43 +0000 | [diff] [blame] | 836 | same type as its argument, so e.g., \code{not 'foo'} yields \code{0}, |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 837 | not \code{''}.) |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 838 | |
| 839 | Lambda forms (lambda expressions) have the same syntactic position as |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 840 | expressions. They are a shorthand to create anonymous functions; the |
| 841 | expression \code{lambda \var{arguments}: \var{expression}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 842 | yields a function object that behaves virtually identical to one |
| 843 | defined with |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 844 | |
| 845 | \begin{verbatim} |
| 846 | def name(arguments): |
| 847 | return expression |
| 848 | \end{verbatim} |
| 849 | |
| 850 | See section \ref{function} for the syntax of parameter lists. Note |
| 851 | that functions created with lambda forms cannot contain statements. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 852 | \label{lambda} |
| 853 | \indexii{lambda}{expression} |
| 854 | \indexii{lambda}{form} |
| 855 | \indexii{anonmymous}{function} |
| 856 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 857 | \strong{Programmer's note:} a lambda form defined inside a function |
| 858 | has no access to names defined in the function's namespace. This is |
| 859 | because Python has only two scopes: local and global. A common |
| 860 | work-around is to use default argument values to pass selected |
| 861 | variables into the lambda's namespace, e.g.: |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 862 | |
| 863 | \begin{verbatim} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 864 | def make_incrementor(increment): |
| 865 | return lambda x, n=increment: x+n |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 866 | \end{verbatim} |
| 867 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 868 | \section{Expression lists\label{exprlists}} |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 869 | \indexii{expression}{list} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 870 | |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 871 | \begin{verbatim} |
| 872 | expression_list: expression ("," expression)* [","] |
| 873 | \end{verbatim} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 874 | |
Fred Drake | c009d19 | 2000-04-25 21:09:10 +0000 | [diff] [blame] | 875 | An expression list containing at least one comma yields a |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 876 | tuple. The length of the tuple is the number of expressions in the |
| 877 | list. The expressions are evaluated from left to right. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 878 | \obindex{tuple} |
| 879 | |
| 880 | The trailing comma is required only to create a single tuple (a.k.a. a |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 881 | \emph{singleton}); it is optional in all other cases. A single |
Fred Drake | c009d19 | 2000-04-25 21:09:10 +0000 | [diff] [blame] | 882 | expression without a trailing comma doesn't create a |
| 883 | tuple, but rather yields the value of that expression. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 884 | (To create an empty tuple, use an empty pair of parentheses: |
Fred Drake | 5c07d9b | 1998-05-14 19:37:06 +0000 | [diff] [blame] | 885 | \code{()}.) |
Guido van Rossum | 3a0ad60 | 1998-07-23 21:57:42 +0000 | [diff] [blame] | 886 | \indexii{trailing}{comma} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 887 | |
Fred Drake | d09120b | 1999-04-29 16:43:42 +0000 | [diff] [blame] | 888 | |
Fred Drake | 020f8c0 | 1998-07-28 19:32:59 +0000 | [diff] [blame] | 889 | \section{Summary\label{summary}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 890 | |
Fred Drake | d09120b | 1999-04-29 16:43:42 +0000 | [diff] [blame] | 891 | The following table summarizes the operator |
| 892 | precedences\indexii{operator}{precedence} in Python, from lowest |
| 893 | precedence (least binding) to highest precedence (most binding). |
| 894 | Operators in the same box have the same precedence. Unless the syntax |
| 895 | is explicitly given, operators are binary. Operators in the same box |
| 896 | group left to right (except for comparisons, which chain from left to |
| 897 | right --- see above). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 898 | |
Fred Drake | d09120b | 1999-04-29 16:43:42 +0000 | [diff] [blame] | 899 | \begin{tableii}{c|l}{textrm}{Operator}{Description} |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 900 | \lineii{\keyword{lambda}} {Lambda expression} |
| 901 | \hline |
| 902 | \lineii{\keyword{or}} {Boolean OR} |
| 903 | \hline |
| 904 | \lineii{\keyword{and}} {Boolean AND} |
| 905 | \hline |
| 906 | \lineii{\keyword{not} \var{x}} {Boolean NOT} |
| 907 | \hline |
| 908 | \lineii{\keyword{in}, \keyword{not} \keyword{in}}{Membership tests} |
| 909 | \lineii{\keyword{is}, \keyword{is not}}{Identity tests} |
| 910 | \lineii{\code{<}, \code{<=}, \code{>}, \code{>=}, |
Fred Drake | 9beee80 | 1998-10-21 00:44:49 +0000 | [diff] [blame] | 911 | \code{<>}, \code{!=}, \code{==}} |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 912 | {Comparisons} |
| 913 | \hline |
| 914 | \lineii{\code{|}} {Bitwise OR} |
| 915 | \hline |
| 916 | \lineii{\code{\^}} {Bitwise XOR} |
| 917 | \hline |
| 918 | \lineii{\code{\&}} {Bitwise AND} |
| 919 | \hline |
| 920 | \lineii{\code{<<}, \code{>>}} {Shifts} |
| 921 | \hline |
| 922 | \lineii{\code{+}, \code{-}}{Addition and subtraction} |
| 923 | \hline |
Fred Drake | 9beee80 | 1998-10-21 00:44:49 +0000 | [diff] [blame] | 924 | \lineii{\code{*}, \code{/}, \code{\%}} |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 925 | {Multiplication, division, remainder} |
| 926 | \hline |
| 927 | \lineii{\code{**}} {Exponentiation} |
| 928 | \hline |
| 929 | \lineii{\code{+\var{x}}, \code{-\var{x}}} {Positive, negative} |
| 930 | \lineii{\code{\~\var{x}}} {Bitwise not} |
| 931 | \hline |
| 932 | \lineii{\code{\var{x}.\var{attribute}}} {Attribute reference} |
| 933 | \lineii{\code{\var{x}[\var{index}]}} {Subscription} |
| 934 | \lineii{\code{\var{x}[\var{index}:\var{index}]}} {Slicing} |
| 935 | \lineii{\code{\var{f}(\var{arguments}...)}} {Function call} |
Fred Drake | d09120b | 1999-04-29 16:43:42 +0000 | [diff] [blame] | 936 | \hline |
Fred Drake | 9ad9c9b | 1998-07-27 20:27:53 +0000 | [diff] [blame] | 937 | \lineii{\code{(\var{expressions}\ldots)}} {Binding or tuple display} |
| 938 | \lineii{\code{[\var{expressions}\ldots]}} {List display} |
| 939 | \lineii{\code{\{\var{key}:\var{datum}\ldots\}}}{Dictionary display} |
| 940 | \lineii{\code{`\var{expressions}\ldots`}} {String conversion} |
| 941 | \end{tableii} |