blob: b6579bc4c8ee7117a3be648ac4989c6f8ae14e5e [file] [log] [blame]
Fred Drake020f8c01998-07-28 19:32:59 +00001\chapter{Expressions\label{expressions}}
Fred Drakef6669171998-05-06 19:52:49 +00002\index{expression}
Fred Drakef6669171998-05-06 19:52:49 +00003
Guido van Rossum3a0ad601998-07-23 21:57:42 +00004This chapter explains the meaning of the elements of expressions in
5Python.
Fred Drakef6669171998-05-06 19:52:49 +00006
Guido van Rossum3a0ad601998-07-23 21:57:42 +00007\strong{Syntax Notes:} In this and the following chapters, extended
8BNF\index{BNF} notation will be used to describe syntax, not lexical
9analysis. When (one alternative of) a syntax rule has the form
Fred Drakef6669171998-05-06 19:52:49 +000010
11\begin{verbatim}
12name: othername
13\end{verbatim}
14
Fred Drake5c07d9b1998-05-14 19:37:06 +000015and no semantics are given, the semantics of this form of \code{name}
16are the same as for \code{othername}.
Fred Drakef6669171998-05-06 19:52:49 +000017\index{syntax}
18
Fred Drake020f8c01998-07-28 19:32:59 +000019\section{Arithmetic conversions\label{conversions}}
Fred Drakef6669171998-05-06 19:52:49 +000020\indexii{arithmetic}{conversion}
21
22When a description of an arithmetic operator below uses the phrase
Guido van Rossum3a0ad601998-07-23 21:57:42 +000023``the numeric arguments are converted to a common type,'' the
24arguments are coerced using the coercion rules listed at the end of
25chapter 3. If both arguments are standard numeric types, the
26following coercions are applied:
Fred Drakef6669171998-05-06 19:52:49 +000027
28\begin{itemize}
Guido van Rossum3a0ad601998-07-23 21:57:42 +000029\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 Drakef6669171998-05-06 19:52:49 +000032 the other is converted to floating point;
Guido van Rossum3a0ad601998-07-23 21:57:42 +000033\item otherwise, if either argument is a long integer,
Fred Drakef6669171998-05-06 19:52:49 +000034 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 Rossum7c0240f1998-07-24 15:36:43 +000039Some additional rules apply for certain operators (e.g., a string left
Guido van Rossum3a0ad601998-07-23 21:57:42 +000040argument to the `\%' operator). Extensions can define their own
41coercions.
Fred Drake020f8c01998-07-28 19:32:59 +000042
43
44\section{Atoms\label{atoms}}
Fred Drakef6669171998-05-06 19:52:49 +000045\index{atom}
46
Guido van Rossum3a0ad601998-07-23 21:57:42 +000047Atoms are the most basic elements of expressions. The simplest atoms
48are identifiers or literals. Forms enclosed in
Fred Drakef6669171998-05-06 19:52:49 +000049reverse quotes or in parentheses, brackets or braces are also
50categorized syntactically as atoms. The syntax for atoms is:
51
52\begin{verbatim}
53atom: identifier | literal | enclosure
54enclosure: parenth_form|list_display|dict_display|string_conversion
55\end{verbatim}
56
Fred Drake020f8c01998-07-28 19:32:59 +000057\subsection{Identifiers (Names)\label{atom-identifiers}}
Fred Drakef6669171998-05-06 19:52:49 +000058\index{name}
59\index{identifier}
60
61An identifier occurring as an atom is a reference to a local, global
62or built-in name binding. If a name is assigned to anywhere in a code
63block (even in unreachable code), and is not mentioned in a
Fred Drake5c07d9b1998-05-14 19:37:06 +000064\keyword{global} statement in that code block, then it refers to a local
Fred Drakef6669171998-05-06 19:52:49 +000065name throughout that code block. When it is not assigned to anywhere
66in the block, or when it is assigned to but also explicitly listed in
Fred Drake5c07d9b1998-05-14 19:37:06 +000067a \keyword{global} statement, it refers to a global name if one exists,
Fred Drakef6669171998-05-06 19:52:49 +000068else 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
75When the name is bound to an object, evaluation of the atom yields
76that object. When a name is not bound, an attempt to evaluate it
Fred Drake5c07d9b1998-05-14 19:37:06 +000077raises a \exception{NameError} exception.
Fred Drakef6669171998-05-06 19:52:49 +000078\exindex{NameError}
79
Guido van Rossum3a0ad601998-07-23 21:57:42 +000080\strong{Private name mangling:}%
81\indexii{name}{mangling}%
82\indexii{private}{names}%
83when an identifier that textually occurs in a class definition begins
84with two or more underscore characters and does not end in two or more
Fred Drakeea81edf1998-11-25 17:51:15 +000085underscores, it is considered a \dfn{private name} of that class.
Guido van Rossum3a0ad601998-07-23 21:57:42 +000086Private names are transformed to a longer form before code is
87generated for them. The transformation inserts the class name in
88front of the name, with leading underscores removed, and a single
89underscore inserted in front of the class name. For example, the
90identifier \code{__spam} occurring in a class named \code{Ham} will be
91transformed to \code{_Ham__spam}. This transformation is independent
92of the syntactical context in which the identifier is used. If the
93transformed name is extremely long (longer than 255 characters),
94implementation defined truncation may happen. If the class name
95consists only of underscores, no transformation is done.
96
Fred Drake020f8c01998-07-28 19:32:59 +000097\subsection{Literals\label{atom-literals}}
Fred Drakef6669171998-05-06 19:52:49 +000098\index{literal}
99
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000100Python supports string literals and various numeric literals:
Fred Drakef6669171998-05-06 19:52:49 +0000101
102\begin{verbatim}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000103literal: stringliteral | integer | longinteger | floatnumber | imagnumber
Fred Drakef6669171998-05-06 19:52:49 +0000104\end{verbatim}
105
106Evaluation of a literal yields an object of the given type (string,
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000107integer, long integer, floating point number, complex number) with the
108given value. The value may be approximated in the case of floating
109point and imaginary (complex) literals. See section \ref{literals}
110for details.
Fred Drakef6669171998-05-06 19:52:49 +0000111
112All literals correspond to immutable data types, and hence the
113object's identity is less important than its value. Multiple
114evaluations of literals with the same value (either the same
115occurrence in the program text or a different occurrence) may obtain
116the same object or a different object with the same value.
117\indexiii{immutable}{data}{type}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000118\indexii{immutable}{objects}
Fred Drakef6669171998-05-06 19:52:49 +0000119
Fred Drake020f8c01998-07-28 19:32:59 +0000120\subsection{Parenthesized forms\label{parenthesized}}
Fred Drakef6669171998-05-06 19:52:49 +0000121\index{parenthesized form}
122
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000123A parenthesized form is an optional expression list enclosed in
Fred Drakef6669171998-05-06 19:52:49 +0000124parentheses:
125
126\begin{verbatim}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000127parenth_form: "(" [expression_list] ")"
Fred Drakef6669171998-05-06 19:52:49 +0000128\end{verbatim}
129
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000130A parenthesized expression list yields whatever that expression list
131yields: if the list contains at least one comma, it yields a tuple;
132otherwise, it yields the single expression that makes up the
133expression list.
Fred Drakef6669171998-05-06 19:52:49 +0000134
135An empty pair of parentheses yields an empty tuple object. Since
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000136tuples are immutable, the rules for literals apply (i.e., two
137occurrences of the empty tuple may or may not yield the same object).
Fred Drakef6669171998-05-06 19:52:49 +0000138\indexii{empty}{tuple}
139
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000140Note that tuples are not formed by the parentheses, but rather by use
Fred Drakef6669171998-05-06 19:52:49 +0000141of the comma operator. The exception is the empty tuple, for which
Fred Drake9ad9c9b1998-07-27 20:27:53 +0000142parentheses \emph{are} required --- allowing unparenthesized ``nothing''
Fred Drakef6669171998-05-06 19:52:49 +0000143in expressions would cause ambiguities and allow common typos to
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000144pass uncaught.
Fred Drakef6669171998-05-06 19:52:49 +0000145\index{comma}
146\indexii{tuple}{display}
147
Fred Drake020f8c01998-07-28 19:32:59 +0000148\subsection{List displays\label{lists}}
Fred Drakef6669171998-05-06 19:52:49 +0000149\indexii{list}{display}
150
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000151A list display is a possibly empty series of expressions enclosed in
Fred Drakef6669171998-05-06 19:52:49 +0000152square brackets:
153
154\begin{verbatim}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000155list_display: "[" [expression_list] "]"
Fred Drakef6669171998-05-06 19:52:49 +0000156\end{verbatim}
157
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000158A list display yields a new list object. If it has no expression
159list, the list object has no items. Otherwise, the elements of the
160expression list are evaluated from left to right and inserted in the
161list object in that order.
Fred Drakef6669171998-05-06 19:52:49 +0000162\obindex{list}
Fred Drakef6669171998-05-06 19:52:49 +0000163\indexii{empty}{list}
164
Fred Drake020f8c01998-07-28 19:32:59 +0000165\subsection{Dictionary displays\label{dict}}
Fred Drakef6669171998-05-06 19:52:49 +0000166\indexii{dictionary}{display}
167
168A dictionary display is a possibly empty series of key/datum pairs
169enclosed in curly braces:
170\index{key}
171\index{datum}
172\index{key/datum pair}
173
174\begin{verbatim}
175dict_display: "{" [key_datum_list] "}"
176key_datum_list: key_datum ("," key_datum)* [","]
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000177key_datum: expression ":" expression
Fred Drakef6669171998-05-06 19:52:49 +0000178\end{verbatim}
179
180A dictionary display yields a new dictionary object.
181\obindex{dictionary}
182
183The key/datum pairs are evaluated from left to right to define the
184entries of the dictionary: each key object is used as a key into the
185dictionary to store the corresponding datum.
186
187Restrictions on the types of the key values are listed earlier in
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000188section \ref{types}. (To summarize,the key type should be hashable,
189which excludes all mutable objects.) Clashes between duplicate keys
190are not detected; the last datum (textually rightmost in the display)
191stored for a given key value prevails.
192\indexii{immutable}{objects}
Fred Drakef6669171998-05-06 19:52:49 +0000193
Fred Drake020f8c01998-07-28 19:32:59 +0000194\subsection{String conversions\label{string-conversions}}
Fred Drakef6669171998-05-06 19:52:49 +0000195\indexii{string}{conversion}
196\indexii{reverse}{quotes}
197\indexii{backward}{quotes}
198\index{back-quotes}
199
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000200A string conversion is an expression list enclosed in reverse (a.k.a.
Fred Drakef6669171998-05-06 19:52:49 +0000201backward) quotes:
202
203\begin{verbatim}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000204string_conversion: "`" expression_list "`"
Fred Drakef6669171998-05-06 19:52:49 +0000205\end{verbatim}
206
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000207A string conversion evaluates the contained expression list and
Fred Drakef6669171998-05-06 19:52:49 +0000208converts the resulting object into a string according to rules
209specific to its type.
210
Fred Drake5c07d9b1998-05-14 19:37:06 +0000211If the object is a string, a number, \code{None}, or a tuple, list or
Fred Drakef6669171998-05-06 19:52:49 +0000212dictionary containing only objects whose type is one of these, the
213resulting string is a valid Python expression which can be passed to
Fred Drake5c07d9b1998-05-14 19:37:06 +0000214the built-in function \function{eval()} to yield an expression with the
Fred Drakef6669171998-05-06 19:52:49 +0000215same value (or an approximation, if floating point numbers are
216involved).
217
218(In particular, converting a string adds quotes around it and converts
219``funny'' characters to escape sequences that are safe to print.)
220
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000221It is illegal to attempt to convert recursive objects (e.g., lists or
Fred Drakef6669171998-05-06 19:52:49 +0000222dictionaries that contain a reference to themselves, directly or
223indirectly.)
224\obindex{recursive}
225
Fred Drake5c07d9b1998-05-14 19:37:06 +0000226The built-in function \function{repr()} performs exactly the same
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000227conversion in its argument as enclosing it in parentheses and reverse
228quotes does. The built-in function \function{str()} performs a
229similar but more user-friendly conversion.
Fred Drakef6669171998-05-06 19:52:49 +0000230\bifuncindex{repr}
231\bifuncindex{str}
232
Fred Drake020f8c01998-07-28 19:32:59 +0000233\section{Primaries\label{primaries}}
Fred Drakef6669171998-05-06 19:52:49 +0000234\index{primary}
235
236Primaries represent the most tightly bound operations of the language.
237Their syntax is:
238
239\begin{verbatim}
240primary: atom | attributeref | subscription | slicing | call
241\end{verbatim}
242
Fred Drake020f8c01998-07-28 19:32:59 +0000243\subsection{Attribute references\label{attribute-references}}
Fred Drakef6669171998-05-06 19:52:49 +0000244\indexii{attribute}{reference}
245
246An attribute reference is a primary followed by a period and a name:
247
248\begin{verbatim}
249attributeref: primary "." identifier
250\end{verbatim}
251
252The primary must evaluate to an object of a type that supports
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000253attribute references, e.g., a module or a list. This object is then
Fred Drakef6669171998-05-06 19:52:49 +0000254asked to produce the attribute whose name is the identifier. If this
Fred Drake5c07d9b1998-05-14 19:37:06 +0000255attribute is not available, the exception
256\exception{AttributeError}\exindex{AttributeError} is raised.
257Otherwise, the type and value of the object produced is determined by
258the object. Multiple evaluations of the same attribute reference may
259yield different objects.
Fred Drakef6669171998-05-06 19:52:49 +0000260\obindex{module}
261\obindex{list}
262
Fred Drake020f8c01998-07-28 19:32:59 +0000263\subsection{Subscriptions\label{subscriptions}}
Fred Drakef6669171998-05-06 19:52:49 +0000264\index{subscription}
265
266A subscription selects an item of a sequence (string, tuple or list)
267or mapping (dictionary) object:
268\obindex{sequence}
269\obindex{mapping}
270\obindex{string}
271\obindex{tuple}
272\obindex{list}
273\obindex{dictionary}
274\indexii{sequence}{item}
275
276\begin{verbatim}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000277subscription: primary "[" expression_list "]"
Fred Drakef6669171998-05-06 19:52:49 +0000278\end{verbatim}
279
280The primary must evaluate to an object of a sequence or mapping type.
281
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000282If the primary is a mapping, the expression list must evaluate to an
283object whose value is one of the keys of the mapping, and the
284subscription selects the value in the mapping that corresponds to that
285key. (The expression list is a tuple except if it has exactly one
286item.)
Fred Drakef6669171998-05-06 19:52:49 +0000287
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000288If the primary is a sequence, the expression (list) must evaluate to a
289plain integer. If this value is negative, the length of the sequence
290is added to it (so that, e.g., \code{x[-1]} selects the last item of
291\code{x}.) The resulting value must be a nonnegative integer less
292than the number of items in the sequence, and the subscription selects
293the item whose index is that value (counting from zero).
Fred Drakef6669171998-05-06 19:52:49 +0000294
295A string's items are characters. A character is not a separate data
296type but a string of exactly one character.
297\index{character}
298\indexii{string}{item}
299
Fred Drake020f8c01998-07-28 19:32:59 +0000300\subsection{Slicings\label{slicings}}
Fred Drakef6669171998-05-06 19:52:49 +0000301\index{slicing}
302\index{slice}
303
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000304A slicing selects a range of items in a sequence object (e.g., a
305string, tuple or list). Slicings may be used as expressions or as
306targets in assignment or del statements. The syntax for a slicing:
Fred Drakef6669171998-05-06 19:52:49 +0000307\obindex{sequence}
308\obindex{string}
309\obindex{tuple}
310\obindex{list}
311
312\begin{verbatim}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000313slicing: simple_slicing | extended_slicing
314simple_slicing: primary "[" short_slice "]"
315extended_slicing: primary "[" slice_list "]"
316slice_list: slice_item ("," slice_item)* [","]
317slice_item: expression | proper_slice | ellipsis
318proper_slice: short_slice | long_slice
319short_slice: [lower_bound] ":" [upper_bound]
320long_slice: short_slice ":" [stride]
321lower_bound: expression
322upper_bound: expression
323stride: expression
324ellipsis: "..."
Fred Drakef6669171998-05-06 19:52:49 +0000325\end{verbatim}
326
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000327There is ambiguity in the formal syntax here: anything that looks like
328an expression list also looks like a slice list, so any subscription
329can be interpreted as a slicing. Rather than further complicating the
330syntax, this is disambiguated by defining that in this case the
331interpretation as a subscription takes priority over the
332interpretation as a slicing (this is the case if the slice list
333contains no proper slice nor ellipses). Similarly, when the slice
334list has exactly one short slice and no trailing comma, the
335interpretation as a simple slicing takes priority over that as an
336extended slicing.\indexii{extended}{slicing}
337
338The semantics for a simple slicing are as follows. The primary must
339evaluate to a sequence object. The lower and upper bound expressions,
340if present, must evaluate to plain integers; defaults are zero and the
341sequence's length, respectively. If either bound is negative, the
342sequence's length is added to it. The slicing now selects all items
343with index \var{k} such that
Fred Drakef6669171998-05-06 19:52:49 +0000344\code{\var{i} <= \var{k} < \var{j}} where \var{i}
345and \var{j} are the specified lower and upper bounds. This may be an
346empty sequence. It is not an error if \var{i} or \var{j} lie outside the
347range of valid indexes (such items don't exist so they aren't
348selected).
349
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000350The semantics for an extended slicing are as follows. The primary
351must evaluate to a mapping object, and it is indexed with a key that
352is constructed from the slice list, as follows. If the slice list
353contains at least one comma, the key is a tuple containing the
354conversion of the slice items; otherwise, the conversion of the lone
355slice item is the key. The conversion of a slice item that is an
356expression is that expression. The conversion of an ellipsis slice
357item is the built-in \code{Ellipsis} object. The conversion of a
358proper slice is a slice object (see section \ref{types}) whose
Fred Drakeea81edf1998-11-25 17:51:15 +0000359\member{start}, \member{stop} and \member{step} attributes are the
360values of the expressions given as lower bound, upper bound and
361stride, respectively, substituting \code{None} for missing
362expressions.
Fred Drake99cd5731999-02-12 20:40:09 +0000363\withsubitem{(slice object attribute)}{\ttindex{start}
364 \ttindex{stop}\ttindex{step}}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000365
Fred Drake020f8c01998-07-28 19:32:59 +0000366\subsection{Calls\label{calls}}
Fred Drakef6669171998-05-06 19:52:49 +0000367\index{call}
368
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000369A call calls a callable object (e.g., a function) with a possibly empty
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000370series of arguments:
Fred Drakef6669171998-05-06 19:52:49 +0000371\obindex{callable}
372
373\begin{verbatim}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000374call: primary "(" [argument_list [","]] ")"
375argument_list: positional_arguments ["," keyword_arguments]
376 | keyword_arguments
377positional_arguments: expression ("," expression)*
378keyword_arguments: keyword_item ("," keyword_item)*
379keyword_item: identifier "=" expression
Fred Drakef6669171998-05-06 19:52:49 +0000380\end{verbatim}
381
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000382A trailing comma may be present after an argument list but does not
383affect the semantics.
384
Fred Drakef6669171998-05-06 19:52:49 +0000385The primary must evaluate to a callable object (user-defined
386functions, built-in functions, methods of built-in objects, class
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000387objects, methods of class instances, and certain class instances
388themselves are callable; extensions may define additional callable
389object types). All argument expressions are evaluated before the call
390is attempted. Please refer to section \ref{function} for the syntax
391of formal parameter lists.
392
393If keyword arguments are present, they are first converted to
394positional arguments, as follows. First, a list of unfilled slots is
395created for the formal parameters. If there are N positional
396arguments, they are placed in the first N slots. Next, for each
397keyword argument, the identifier is used to determine the
398corresponding slot (if the identifier is the same as the first formal
399parameter name, the first slot is used, and so on). If the slot is
400already filled, a \exception{TypeError} exception is raised.
401Otherwise, the value of the argument is placed in the slot, filling it
402(even if the expression is \code{None}, it fills the slot). When all
403arguments have been processed, the slots that are still unfilled are
404filled with the corresponding default value from the function
405definition. (Default values are calculated, once, when the function
406is defined; thus, a mutable object such as a list or dictionary used
407as default value will be shared by all calls that don't specify an
408argument value for the corresponding slot; this should usually be
409avoided.) If there are any unfilled slots for which no default value
410is specified, a \exception{TypeError} exception is raised. Otherwise,
411the list of filled slots is used as the argument list for the call.
412
413If there are more positional arguments than there are formal parameter
414slots, a \exception{TypeError} exception is raised, unless a formal
Fred Drakeea81edf1998-11-25 17:51:15 +0000415parameter using the syntax \samp{*identifier} is present; in this
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000416case, that formal parameter receives a tuple containing the excess
417positional arguments (or an empty tuple if there were no excess
418positional arguments).
419
420If any keyword argument does not correspond to a formal parameter
421name, a \exception{TypeError} exception is raised, unless a formal
Fred Drakeea81edf1998-11-25 17:51:15 +0000422parameter using the syntax \samp{**identifier} is present; in this
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000423case, that formal parameter receives a dictionary containing the
424excess keyword arguments (using the keywords as keys and the argument
425values as corresponding values), or a (new) empty dictionary if there
426were no excess keyword arguments.
427
Fred Drakeea81edf1998-11-25 17:51:15 +0000428Formal parameters using the syntax \samp{*identifier} or
429\samp{**identifier} cannot be used as positional argument slots or
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000430as keyword argument names. Formal parameters using the syntax
Fred Drakeea81edf1998-11-25 17:51:15 +0000431\samp{(sublist)} cannot be used as keyword argument names; the
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000432outermost sublist corresponds to a single unnamed argument slot, and
433the argument value is assigned to the sublist using the usual tuple
434assignment rules after all other parameter processing is done.
Fred Drakef6669171998-05-06 19:52:49 +0000435
Fred Drake5c07d9b1998-05-14 19:37:06 +0000436A call always returns some value, possibly \code{None}, unless it
Fred Drakef6669171998-05-06 19:52:49 +0000437raises an exception. How this value is computed depends on the type
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000438of the callable object.
439
440If it is---
Fred Drakef6669171998-05-06 19:52:49 +0000441
442\begin{description}
443
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000444\item[a user-defined function:] The code block for the function is
Fred Drakef6669171998-05-06 19:52:49 +0000445executed, passing it the argument list. The first thing the code
446block will do is bind the formal parameters to the arguments; this is
447described in section \ref{function}. When the code block executes a
Fred Drake5c07d9b1998-05-14 19:37:06 +0000448\keyword{return} statement, this specifies the return value of the
Fred Drakef6669171998-05-06 19:52:49 +0000449function call.
450\indexii{function}{call}
451\indexiii{user-defined}{function}{call}
452\obindex{user-defined function}
453\obindex{function}
454
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000455\item[a built-in function or method:] The result is up to the
Fred Drakef6669171998-05-06 19:52:49 +0000456interpreter; see the library reference manual for the descriptions of
457built-in functions and methods.
458\indexii{function}{call}
459\indexii{built-in function}{call}
460\indexii{method}{call}
461\indexii{built-in method}{call}
462\obindex{built-in method}
463\obindex{built-in function}
464\obindex{method}
465\obindex{function}
466
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000467\item[a class object:] A new instance of that class is returned.
Fred Drakef6669171998-05-06 19:52:49 +0000468\obindex{class}
469\indexii{class object}{call}
470
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000471\item[a class instance method:] The corresponding user-defined
Fred Drakef6669171998-05-06 19:52:49 +0000472function is called, with an argument list that is one longer than the
473argument list of the call: the instance becomes the first argument.
474\obindex{class instance}
475\obindex{instance}
Fred Drakef6669171998-05-06 19:52:49 +0000476\indexii{class instance}{call}
477
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000478\item[a class instance:] The class must define a \method{__call__()}
479method; the effect is then the same as if that method was called.
480\indexii{instance}{call}
Fred Drakeea81edf1998-11-25 17:51:15 +0000481\withsubitem{(object method)}{\ttindex{__call__()}}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000482
Fred Drakef6669171998-05-06 19:52:49 +0000483\end{description}
484
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000485
Fred Drake020f8c01998-07-28 19:32:59 +0000486\section{The power operator\label{power}}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000487
488The power operator binds more tightly than unary operators on its
489left; it binds less tightly than unary operators on its right. The
490syntax is:
491
492\begin{verbatim}
493power: primary ["**" u_expr]
494\end{verbatim}
495
496Thus, in an unparenthesized sequence of power and unary operators, the
497operators are evaluated from right to left (this does not constrain
498the evaluation order for the operands).
499
500The power operator has the same semantics as the built-in
501\function{pow()} function, when called with two arguments: it yields
502its left argument raised to the power of its right argument. The
503numeric arguments are first converted to a common type. The result
504type is that of the arguments after coercion; if the result is not
505expressible in that type (as in raising an integer to a negative
506power, or a negative floating point number to a broken power), a
507\exception{TypeError} exception is raised.
508
509
Fred Drake020f8c01998-07-28 19:32:59 +0000510\section{Unary arithmetic operations\label{unary}}
Fred Drakef6669171998-05-06 19:52:49 +0000511\indexiii{unary}{arithmetic}{operation}
512\indexiii{unary}{bit-wise}{operation}
513
514All unary arithmetic (and bit-wise) operations have the same priority:
515
516\begin{verbatim}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000517u_expr: power | "-" u_expr | "+" u_expr | "~" u_expr
Fred Drakef6669171998-05-06 19:52:49 +0000518\end{verbatim}
519
Fred Drake5c07d9b1998-05-14 19:37:06 +0000520The unary \code{-} (minus) operator yields the negation of its
Fred Drakef6669171998-05-06 19:52:49 +0000521numeric argument.
522\index{negation}
523\index{minus}
524
Fred Drake5c07d9b1998-05-14 19:37:06 +0000525The unary \code{+} (plus) operator yields its numeric argument
Fred Drakef6669171998-05-06 19:52:49 +0000526unchanged.
527\index{plus}
528
Fred Drake5c07d9b1998-05-14 19:37:06 +0000529The unary \code{~} (invert) operator yields the bit-wise inversion
Fred Drakef6669171998-05-06 19:52:49 +0000530of its plain or long integer argument. The bit-wise inversion of
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000531\code{x} is defined as \code{-(x+1)}. It only applies to integral
532numbers.
Fred Drakef6669171998-05-06 19:52:49 +0000533\index{inversion}
534
535In all three cases, if the argument does not have the proper type,
Fred Drake5c07d9b1998-05-14 19:37:06 +0000536a \exception{TypeError} exception is raised.
Fred Drakef6669171998-05-06 19:52:49 +0000537\exindex{TypeError}
538
Fred Drake020f8c01998-07-28 19:32:59 +0000539\section{Binary arithmetic operations\label{binary}}
Fred Drakef6669171998-05-06 19:52:49 +0000540\indexiii{binary}{arithmetic}{operation}
541
542The binary arithmetic operations have the conventional priority
543levels. Note that some of these operations also apply to certain
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000544non-numeric types. Apart from the power operator, there are only two
545levels, one for multiplicative operators and one for additive
Fred Drakef6669171998-05-06 19:52:49 +0000546operators:
547
548\begin{verbatim}
549m_expr: u_expr | m_expr "*" u_expr
550 | m_expr "/" u_expr | m_expr "%" u_expr
551a_expr: m_expr | aexpr "+" m_expr | aexpr "-" m_expr
552\end{verbatim}
553
Fred Drake5c07d9b1998-05-14 19:37:06 +0000554The \code{*} (multiplication) operator yields the product of its
Fred Drakef6669171998-05-06 19:52:49 +0000555arguments. The arguments must either both be numbers, or one argument
556must be a plain integer and the other must be a sequence. In the
557former case, the numbers are converted to a common type and then
558multiplied together. In the latter case, sequence repetition is
559performed; a negative repetition factor yields an empty sequence.
560\index{multiplication}
561
Fred Drake5c07d9b1998-05-14 19:37:06 +0000562The \code{/} (division) operator yields the quotient of its
Fred Drakef6669171998-05-06 19:52:49 +0000563arguments. The numeric arguments are first converted to a common
564type. Plain or long integer division yields an integer of the same
565type; the result is that of mathematical division with the `floor'
566function applied to the result. Division by zero raises the
Fred Drake5c07d9b1998-05-14 19:37:06 +0000567\exception{ZeroDivisionError} exception.
Fred Drakef6669171998-05-06 19:52:49 +0000568\exindex{ZeroDivisionError}
569\index{division}
570
Fred Drake5c07d9b1998-05-14 19:37:06 +0000571The \code{\%} (modulo) operator yields the remainder from the
Fred Drakef6669171998-05-06 19:52:49 +0000572division of the first argument by the second. The numeric arguments
573are first converted to a common type. A zero right argument raises
Fred Drake5c07d9b1998-05-14 19:37:06 +0000574the \exception{ZeroDivisionError} exception. The arguments may be floating
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000575point numbers, e.g., \code{3.14\%0.7} equals \code{0.34} (since
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000576\code{3.14} equals \code{4*0.7 + 0.34}.) The modulo operator always
577yields a result with the same sign as its second operand (or zero);
578the absolute value of the result is strictly smaller than the second
579operand.
Fred Drakef6669171998-05-06 19:52:49 +0000580\index{modulo}
581
582The integer division and modulo operators are connected by the
Fred Drake5c07d9b1998-05-14 19:37:06 +0000583following identity: \code{x == (x/y)*y + (x\%y)}. Integer division and
584modulo are also connected with the built-in function \function{divmod()}:
585\code{divmod(x, y) == (x/y, x\%y)}. These identities don't hold for
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000586floating point and complex numbers; there a similar identity holds where
587\code{x/y} is replaced by \code{floor(x/y)}) or
588\code{floor((x/y).real)}, respectively.
Fred Drakef6669171998-05-06 19:52:49 +0000589
Fred Drake5c07d9b1998-05-14 19:37:06 +0000590The \code{+} (addition) operator yields the sum of its arguments.
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000591The arguments must either both be numbers or both sequences of the
Fred Drakef6669171998-05-06 19:52:49 +0000592same type. In the former case, the numbers are converted to a common
593type and then added together. In the latter case, the sequences are
594concatenated.
595\index{addition}
596
Fred Drake5c07d9b1998-05-14 19:37:06 +0000597The \code{-} (subtraction) operator yields the difference of its
Fred Drakef6669171998-05-06 19:52:49 +0000598arguments. The numeric arguments are first converted to a common
599type.
600\index{subtraction}
601
Fred Drake020f8c01998-07-28 19:32:59 +0000602\section{Shifting operations\label{shifting}}
Fred Drakef6669171998-05-06 19:52:49 +0000603\indexii{shifting}{operation}
604
605The shifting operations have lower priority than the arithmetic
606operations:
607
608\begin{verbatim}
609shift_expr: a_expr | shift_expr ( "<<" | ">>" ) a_expr
610\end{verbatim}
611
612These operators accept plain or long integers as arguments. The
613arguments are converted to a common type. They shift the first
614argument to the left or right by the number of bits given by the
615second argument.
616
617A right shift by \var{n} bits is defined as division by
618\code{pow(2,\var{n})}. A left shift by \var{n} bits is defined as
619multiplication with \code{pow(2,\var{n})}; for plain integers there is
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000620no overflow check so in that case the operation drops bits and flips
621the sign if the result is not less than \code{pow(2,31)} in absolute
622value. Negative shift counts raise a \exception{ValueError}
623exception.
Fred Drakef6669171998-05-06 19:52:49 +0000624\exindex{ValueError}
625
Fred Drake020f8c01998-07-28 19:32:59 +0000626\section{Binary bit-wise operations\label{bitwise}}
Fred Drakef6669171998-05-06 19:52:49 +0000627\indexiii{binary}{bit-wise}{operation}
628
629Each of the three bitwise operations has a different priority level:
630
631\begin{verbatim}
632and_expr: shift_expr | and_expr "&" shift_expr
633xor_expr: and_expr | xor_expr "^" and_expr
634or_expr: xor_expr | or_expr "|" xor_expr
635\end{verbatim}
636
Fred Drake5c07d9b1998-05-14 19:37:06 +0000637The \code{\&} operator yields the bitwise AND of its arguments, which
Fred Drakef6669171998-05-06 19:52:49 +0000638must be plain or long integers. The arguments are converted to a
639common type.
640\indexii{bit-wise}{and}
641
Fred Drake5c07d9b1998-05-14 19:37:06 +0000642The \code{\^} operator yields the bitwise XOR (exclusive OR) of its
Fred Drakef6669171998-05-06 19:52:49 +0000643arguments, which must be plain or long integers. The arguments are
644converted to a common type.
645\indexii{bit-wise}{xor}
646\indexii{exclusive}{or}
647
Fred Drake5c07d9b1998-05-14 19:37:06 +0000648The \code{|} operator yields the bitwise (inclusive) OR of its
Fred Drakef6669171998-05-06 19:52:49 +0000649arguments, which must be plain or long integers. The arguments are
650converted to a common type.
651\indexii{bit-wise}{or}
652\indexii{inclusive}{or}
653
Fred Drake020f8c01998-07-28 19:32:59 +0000654\section{Comparisons\label{comparisons}}
Fred Drakef6669171998-05-06 19:52:49 +0000655\index{comparison}
656
Fred Drake5c07d9b1998-05-14 19:37:06 +0000657Contrary to \C, all comparison operations in Python have the same
Fred Drakef6669171998-05-06 19:52:49 +0000658priority, which is lower than that of any arithmetic, shifting or
Fred Drake5c07d9b1998-05-14 19:37:06 +0000659bitwise operation. Also contrary to \C, expressions like
660\code{a < b < c} have the interpretation that is conventional in
Fred Drakef6669171998-05-06 19:52:49 +0000661mathematics:
Fred Drake5c07d9b1998-05-14 19:37:06 +0000662\indexii{C}{language}
Fred Drakef6669171998-05-06 19:52:49 +0000663
664\begin{verbatim}
665comparison: or_expr (comp_operator or_expr)*
666comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in"
667\end{verbatim}
668
Fred Drake5c07d9b1998-05-14 19:37:06 +0000669Comparisons yield integer values: \code{1} for true, \code{0} for false.
Fred Drakef6669171998-05-06 19:52:49 +0000670
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000671Comparisons can be chained arbitrarily, e.g., \code{x < y <= z} is
Fred Drakef6669171998-05-06 19:52:49 +0000672equivalent to \code{x < y and y <= z}, except that \code{y} is
673evaluated only once (but in both cases \code{z} is not evaluated at all
674when \code{x < y} is found to be false).
675\indexii{chaining}{comparisons}
676
677Formally, if \var{a}, \var{b}, \var{c}, \ldots, \var{y}, \var{z} are
678expressions and \var{opa}, \var{opb}, \ldots, \var{opy} are comparison
679operators, then \var{a opa b opb c} \ldots \var{y opy z} is equivalent
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000680to \var{a opa b} \keyword{and} \var{b opb c} \keyword{and} \ldots
Fred Drakef6669171998-05-06 19:52:49 +0000681\var{y opy z}, except that each expression is evaluated at most once.
682
683Note that \var{a opa b opb c} doesn't imply any kind of comparison
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000684between \var{a} and \var{c}, so that, e.g., \code{x < y > z} is
Fred Drakef6669171998-05-06 19:52:49 +0000685perfectly legal (though perhaps not pretty).
686
Fred Drake5c07d9b1998-05-14 19:37:06 +0000687The forms \code{<>} and \code{!=} are equivalent; for consistency with
688C, \code{!=} is preferred; where \code{!=} is mentioned below
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000689\code{<>} is also acceptable. At some point in the (far) future,
690\code{<>} may become obsolete.
Fred Drakef6669171998-05-06 19:52:49 +0000691
Fred Draked03268f1998-11-25 19:23:33 +0000692The operators \texttt{"<", ">", "==", ">=", "<="}, and \texttt{"!="} compare
Fred Drakef6669171998-05-06 19:52:49 +0000693the values of two objects. The objects needn't have the same type.
694If both are numbers, they are coverted to a common type. Otherwise,
Fred Drake9ad9c9b1998-07-27 20:27:53 +0000695objects of different types \emph{always} compare unequal, and are
Fred Drakef6669171998-05-06 19:52:49 +0000696ordered consistently but arbitrarily.
697
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000698(This unusual definition of comparison was used to simplify the
Fred Drake5c07d9b1998-05-14 19:37:06 +0000699definition of operations like sorting and the \keyword{in} and
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000700\keyword{not in} operators. In the future, the comparison rules for
701objects of different types are likely to change.)
Fred Drakef6669171998-05-06 19:52:49 +0000702
703Comparison of objects of the same type depends on the type:
704
705\begin{itemize}
706
707\item
708Numbers are compared arithmetically.
709
710\item
711Strings are compared lexicographically using the numeric equivalents
Fred Drake5c07d9b1998-05-14 19:37:06 +0000712(the result of the built-in function \function{ord()}) of their
713characters.
Fred Drakef6669171998-05-06 19:52:49 +0000714
715\item
716Tuples and lists are compared lexicographically using comparison of
717corresponding items.
718
719\item
720Mappings (dictionaries) are compared through lexicographic
721comparison of their sorted (key, value) lists.%
722\footnote{This is expensive since it requires sorting the keys first,
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000723but it is about the only sensible definition. An earlier version of
724Python compared dictionaries by identity only, but this caused
725surprises because people expected to be able to test a dictionary for
726emptiness by comparing it to \code{\{\}}.}
Fred Drakef6669171998-05-06 19:52:49 +0000727
728\item
729Most other types compare unequal unless they are the same object;
730the choice whether one object is considered smaller or larger than
731another one is made arbitrarily but consistently within one
732execution of a program.
733
734\end{itemize}
735
Fred Drake5c07d9b1998-05-14 19:37:06 +0000736The operators \keyword{in} and \keyword{not in} test for sequence
Fred Drakef6669171998-05-06 19:52:49 +0000737membership: if \var{y} is a sequence, \code{\var{x} in \var{y}} is
738true if and only if there exists an index \var{i} such that
739\code{\var{x} = \var{y}[\var{i}]}.
740\code{\var{x} not in \var{y}} yields the inverse truth value. The
Fred Drake5c07d9b1998-05-14 19:37:06 +0000741exception \exception{TypeError} is raised when \var{y} is not a sequence,
Fred Drakef6669171998-05-06 19:52:49 +0000742or when \var{y} is a string and \var{x} is not a string of length one.%
743\footnote{The latter restriction is sometimes a nuisance.}
744\opindex{in}
745\opindex{not in}
746\indexii{membership}{test}
747\obindex{sequence}
748
Fred Drake5c07d9b1998-05-14 19:37:06 +0000749The operators \keyword{is} and \keyword{is not} test for object identity:
750\code{\var{x} is \var{y}} is true if and only if \var{x} and \var{y}
751are the same object. \code{\var{x} is not \var{y}} yields the inverse
Fred Drakef6669171998-05-06 19:52:49 +0000752truth value.
753\opindex{is}
754\opindex{is not}
755\indexii{identity}{test}
756
Fred Drake020f8c01998-07-28 19:32:59 +0000757\section{Boolean operations\label{Booleans}}
Fred Drakef6669171998-05-06 19:52:49 +0000758\indexii{Boolean}{operation}
759
760Boolean operations have the lowest priority of all Python operations:
761
762\begin{verbatim}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000763expression: or_test | lambda_form
Fred Drakef6669171998-05-06 19:52:49 +0000764or_test: and_test | or_test "or" and_test
765and_test: not_test | and_test "and" not_test
766not_test: comparison | "not" not_test
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000767lambda_form: "lambda" [parameter_list]: expression
Fred Drakef6669171998-05-06 19:52:49 +0000768\end{verbatim}
769
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000770In the context of Boolean operations, and also when expressions are
Fred Drakef6669171998-05-06 19:52:49 +0000771used by control flow statements, the following values are interpreted
Fred Drake5c07d9b1998-05-14 19:37:06 +0000772as false: \code{None}, numeric zero of all types, empty sequences
Fred Drakef6669171998-05-06 19:52:49 +0000773(strings, tuples and lists), and empty mappings (dictionaries). All
774other values are interpreted as true.
775
Fred Drake5c07d9b1998-05-14 19:37:06 +0000776The operator \keyword{not} yields \code{1} if its argument is false,
777\code{0} otherwise.
Fred Drakef6669171998-05-06 19:52:49 +0000778\opindex{not}
779
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000780The expression \code{\var{x} and \var{y}} first evaluates \var{x}; if
Fred Drakef6669171998-05-06 19:52:49 +0000781\var{x} is false, its value is returned; otherwise, \var{y} is
782evaluated and the resulting value is returned.
783\opindex{and}
784
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000785The expression \code{\var{x} or \var{y}} first evaluates \var{x}; if
Fred Drakef6669171998-05-06 19:52:49 +0000786\var{x} is true, its value is returned; otherwise, \var{y} is
787evaluated and the resulting value is returned.
788\opindex{or}
789
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000790(Note that neither \keyword{and} nor \keyword{or} restrict the value
Fred Drake5c07d9b1998-05-14 19:37:06 +0000791and type they return to \code{0} and \code{1}, but rather return the
792last evaluated argument.
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000793This is sometimes useful, e.g., if \code{s} is a string that should be
Fred Drakef6669171998-05-06 19:52:49 +0000794replaced by a default value if it is empty, the expression
Fred Drake5c07d9b1998-05-14 19:37:06 +0000795\code{s or 'foo'} yields the desired value. Because \keyword{not} has to
Fred Drakef6669171998-05-06 19:52:49 +0000796invent a value anyway, it does not bother to return a value of the
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000797same type as its argument, so e.g., \code{not 'foo'} yields \code{0},
Fred Drake5c07d9b1998-05-14 19:37:06 +0000798not \code{''}.)
Fred Drakef6669171998-05-06 19:52:49 +0000799
800Lambda forms (lambda expressions) have the same syntactic position as
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000801expressions. They are a shorthand to create anonymous functions; the
802expression \code{lambda \var{arguments}: \var{expression}}
Fred Drakef6669171998-05-06 19:52:49 +0000803yields a function object that behaves virtually identical to one
804defined with
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000805
806\begin{verbatim}
807def name(arguments):
808 return expression
809\end{verbatim}
810
811See section \ref{function} for the syntax of parameter lists. Note
812that functions created with lambda forms cannot contain statements.
Fred Drakef6669171998-05-06 19:52:49 +0000813\label{lambda}
814\indexii{lambda}{expression}
815\indexii{lambda}{form}
816\indexii{anonmymous}{function}
817
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000818\strong{Programmer's note:} a lambda form defined inside a function
819has no access to names defined in the function's namespace. This is
820because Python has only two scopes: local and global. A common
821work-around is to use default argument values to pass selected
822variables into the lambda's namespace, e.g.:
Fred Drakef6669171998-05-06 19:52:49 +0000823
824\begin{verbatim}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000825def make_incrementor(increment):
826 return lambda x, n=increment: x+n
Fred Drakef6669171998-05-06 19:52:49 +0000827\end{verbatim}
828
Fred Drake020f8c01998-07-28 19:32:59 +0000829\section{Expression lists\label{exprlists}}
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000830\indexii{expression}{list}
Fred Drakef6669171998-05-06 19:52:49 +0000831
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000832\begin{verbatim}
833expression_list: expression ("," expression)* [","]
834\end{verbatim}
Fred Drakef6669171998-05-06 19:52:49 +0000835
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000836An expression (expression) list containing at least one comma yields a
837tuple. The length of the tuple is the number of expressions in the
838list. The expressions are evaluated from left to right.
Fred Drakef6669171998-05-06 19:52:49 +0000839\obindex{tuple}
840
841The trailing comma is required only to create a single tuple (a.k.a. a
Fred Drake9ad9c9b1998-07-27 20:27:53 +0000842\emph{singleton}); it is optional in all other cases. A single
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000843expression (expression) without a trailing comma doesn't create a
844tuple, but rather yields the value of that expression (expression).
Fred Drakef6669171998-05-06 19:52:49 +0000845(To create an empty tuple, use an empty pair of parentheses:
Fred Drake5c07d9b1998-05-14 19:37:06 +0000846\code{()}.)
Guido van Rossum3a0ad601998-07-23 21:57:42 +0000847\indexii{trailing}{comma}
Fred Drakef6669171998-05-06 19:52:49 +0000848
Fred Drake020f8c01998-07-28 19:32:59 +0000849\section{Summary\label{summary}}
Fred Drakef6669171998-05-06 19:52:49 +0000850
851The following table summarizes the operator precedences in Python,
852from lowest precedence (least binding) to highest precedence (most
853binding). Operators in the same box have the same precedence. Unless
854the syntax is explicitly given, operators are binary. Operators in
855the same box group left to right (except for comparisons, which
856chain from left to right --- see above).
857
Fred Drake9ad9c9b1998-07-27 20:27:53 +0000858\begin{tableii}{c|c}{textrm}{Operator}{Description}
859 \lineii{\keyword{lambda}} {Lambda expression}
860 \hline
861 \lineii{\keyword{or}} {Boolean OR}
862 \hline
863 \lineii{\keyword{and}} {Boolean AND}
864 \hline
865 \lineii{\keyword{not} \var{x}} {Boolean NOT}
866 \hline
867 \lineii{\keyword{in}, \keyword{not} \keyword{in}}{Membership tests}
868 \lineii{\keyword{is}, \keyword{is not}}{Identity tests}
869 \lineii{\code{<}, \code{<=}, \code{>}, \code{>=},
Fred Drake9beee801998-10-21 00:44:49 +0000870 \code{<>}, \code{!=}, \code{==}}
Fred Drake9ad9c9b1998-07-27 20:27:53 +0000871 {Comparisons}
872 \hline
873 \lineii{\code{|}} {Bitwise OR}
874 \hline
875 \lineii{\code{\^}} {Bitwise XOR}
876 \hline
877 \lineii{\code{\&}} {Bitwise AND}
878 \hline
879 \lineii{\code{<<}, \code{>>}} {Shifts}
880 \hline
881 \lineii{\code{+}, \code{-}}{Addition and subtraction}
882 \hline
Fred Drake9beee801998-10-21 00:44:49 +0000883 \lineii{\code{*}, \code{/}, \code{\%}}
Fred Drake9ad9c9b1998-07-27 20:27:53 +0000884 {Multiplication, division, remainder}
885 \hline
886 \lineii{\code{**}} {Exponentiation}
887 \hline
888 \lineii{\code{+\var{x}}, \code{-\var{x}}} {Positive, negative}
889 \lineii{\code{\~\var{x}}} {Bitwise not}
890 \hline
891 \lineii{\code{\var{x}.\var{attribute}}} {Attribute reference}
892 \lineii{\code{\var{x}[\var{index}]}} {Subscription}
893 \lineii{\code{\var{x}[\var{index}:\var{index}]}} {Slicing}
894 \lineii{\code{\var{f}(\var{arguments}...)}} {Function call}
Fred Drakef6669171998-05-06 19:52:49 +0000895\hline
Fred Drake9ad9c9b1998-07-27 20:27:53 +0000896 \lineii{\code{(\var{expressions}\ldots)}} {Binding or tuple display}
897 \lineii{\code{[\var{expressions}\ldots]}} {List display}
898 \lineii{\code{\{\var{key}:\var{datum}\ldots\}}}{Dictionary display}
899 \lineii{\code{`\var{expressions}\ldots`}} {String conversion}
900\end{tableii}