blob: 6650b99098453e1a18d724964204734f1e5d90dc [file] [log] [blame]
Fred Drakea1cce711998-07-24 22:12:32 +00001\chapter{Lexical analysis\label{lexical}}
Fred Drakef6669171998-05-06 19:52:49 +00002
Fred Drake5c07d9b1998-05-14 19:37:06 +00003A Python program is read by a \emph{parser}. Input to the parser is a
4stream of \emph{tokens}, generated by the \emph{lexical analyzer}. This
Fred Drakef6669171998-05-06 19:52:49 +00005chapter describes how the lexical analyzer breaks a file into tokens.
6\index{lexical analysis}
7\index{parser}
8\index{token}
9
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000010Python uses the 7-bit \ASCII{} character set for program text and string
11literals. 8-bit characters may be used in string literals and comments
12but their interpretation is platform dependent; the proper way to
13insert 8-bit characters in string literals is by using octal or
14hexadecimal escape sequences.
15
16The run-time character set depends on the I/O devices connected to the
17program but is generally a superset of \ASCII{}.
18
19\strong{Future compatibility note:} It may be tempting to assume that the
20character set for 8-bit characters is ISO Latin-1 (an \ASCII{}
21superset that covers most western languages that use the Latin
22alphabet), but it is possible that in the future Unicode text editors
23will become common. These generally use the UTF-8 encoding, which is
24also an \ASCII{} superset, but with very different use for the
25characters with ordinals 128-255. While there is no consensus on this
26subject yet, it is unwise to assume either Latin-1 or UTF-8, even
27though the current implementation appears to favor Latin-1. This
28applies both to the source character set and the run-time character
29set.
30
Fred Drakef5eae662001-06-23 05:26:52 +000031
Fred Drake61c77281998-07-28 19:34:22 +000032\section{Line structure\label{line-structure}}
Fred Drakef6669171998-05-06 19:52:49 +000033
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000034A Python program is divided into a number of \emph{logical lines}.
35\index{line structure}
36
Fred Drakef5eae662001-06-23 05:26:52 +000037
Fred Drake61c77281998-07-28 19:34:22 +000038\subsection{Logical lines\label{logical}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000039
40The end of
Fred Drakef6669171998-05-06 19:52:49 +000041a logical line is represented by the token NEWLINE. Statements cannot
42cross logical line boundaries except where NEWLINE is allowed by the
Guido van Rossum7c0240f1998-07-24 15:36:43 +000043syntax (e.g., between statements in compound statements).
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000044A logical line is constructed from one or more \emph{physical lines}
45by following the explicit or implicit \emph{line joining} rules.
Fred Drakef6669171998-05-06 19:52:49 +000046\index{logical line}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000047\index{physical line}
48\index{line joining}
Fred Drakef6669171998-05-06 19:52:49 +000049\index{NEWLINE token}
50
Fred Drakef5eae662001-06-23 05:26:52 +000051
Fred Drake61c77281998-07-28 19:34:22 +000052\subsection{Physical lines\label{physical}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000053
54A physical line ends in whatever the current platform's convention is
55for terminating lines. On \UNIX{}, this is the \ASCII{} LF (linefeed)
56character. On DOS/Windows, it is the \ASCII{} sequence CR LF (return
57followed by linefeed). On Macintosh, it is the \ASCII{} CR (return)
58character.
59
Fred Drakef5eae662001-06-23 05:26:52 +000060
Fred Drake61c77281998-07-28 19:34:22 +000061\subsection{Comments\label{comments}}
Fred Drakef6669171998-05-06 19:52:49 +000062
Fred Drake5c07d9b1998-05-14 19:37:06 +000063A comment starts with a hash character (\code{\#}) that is not part of
Fred Drakef6669171998-05-06 19:52:49 +000064a string literal, and ends at the end of the physical line. A comment
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000065signifies the end of the logical line unless the implicit line joining
66rules are invoked.
67Comments are ignored by the syntax; they are not tokens.
Fred Drakef6669171998-05-06 19:52:49 +000068\index{comment}
Fred Drakef6669171998-05-06 19:52:49 +000069\index{hash character}
70
Fred Drakef5eae662001-06-23 05:26:52 +000071
Fred Drake61c77281998-07-28 19:34:22 +000072\subsection{Explicit line joining\label{explicit-joining}}
Fred Drakef6669171998-05-06 19:52:49 +000073
74Two or more physical lines may be joined into logical lines using
Fred Drake5c07d9b1998-05-14 19:37:06 +000075backslash characters (\code{\e}), as follows: when a physical line ends
Fred Drakef6669171998-05-06 19:52:49 +000076in a backslash that is not part of a string literal or comment, it is
77joined with the following forming a single logical line, deleting the
78backslash and the following end-of-line character. For example:
79\index{physical line}
80\index{line joining}
81\index{line continuation}
82\index{backslash character}
83%
84\begin{verbatim}
85if 1900 < year < 2100 and 1 <= month <= 12 \
86 and 1 <= day <= 31 and 0 <= hour < 24 \
87 and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date
88 return 1
89\end{verbatim}
90
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000091A line ending in a backslash cannot carry a comment. A backslash does
92not continue a comment. A backslash does not continue a token except
93for string literals (i.e., tokens other than string literals cannot be
94split across physical lines using a backslash). A backslash is
95illegal elsewhere on a line outside a string literal.
Fred Drakef6669171998-05-06 19:52:49 +000096
Fred Drakec411fa61999-02-22 14:32:18 +000097
Fred Drake61c77281998-07-28 19:34:22 +000098\subsection{Implicit line joining\label{implicit-joining}}
Fred Drakef6669171998-05-06 19:52:49 +000099
100Expressions in parentheses, square brackets or curly braces can be
101split over more than one physical line without using backslashes.
102For example:
103
104\begin{verbatim}
105month_names = ['Januari', 'Februari', 'Maart', # These are the
106 'April', 'Mei', 'Juni', # Dutch names
107 'Juli', 'Augustus', 'September', # for the months
108 'Oktober', 'November', 'December'] # of the year
109\end{verbatim}
110
111Implicitly continued lines can carry comments. The indentation of the
112continuation lines is not important. Blank continuation lines are
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000113allowed. There is no NEWLINE token between implicit continuation
114lines. Implicitly continued lines can also occur within triple-quoted
115strings (see below); in that case they cannot carry comments.
Fred Drakef6669171998-05-06 19:52:49 +0000116
Fred Drakef6669171998-05-06 19:52:49 +0000117
Fred Drakec411fa61999-02-22 14:32:18 +0000118\subsection{Blank lines \index{blank line}\label{blank-lines}}
119
120A logical line that contains only spaces, tabs, formfeeds and possibly
121a comment, is ignored (i.e., no NEWLINE token is generated). During
122interactive input of statements, handling of a blank line may differ
123depending on the implementation of the read-eval-print loop. In the
124standard implementation, an entirely blank logical line (i.e.\ one
125containing not even whitespace or a comment) terminates a multi-line
126statement.
127
Fred Drakef6669171998-05-06 19:52:49 +0000128
Fred Drake61c77281998-07-28 19:34:22 +0000129\subsection{Indentation\label{indentation}}
Fred Drakef6669171998-05-06 19:52:49 +0000130
131Leading whitespace (spaces and tabs) at the beginning of a logical
132line is used to compute the indentation level of the line, which in
133turn is used to determine the grouping of statements.
134\index{indentation}
135\index{whitespace}
136\index{leading whitespace}
137\index{space}
138\index{tab}
139\index{grouping}
140\index{statement grouping}
141
142First, tabs are replaced (from left to right) by one to eight spaces
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000143such that the total number of characters up to and including the
144replacement is a multiple of
Fred Drake5c07d9b1998-05-14 19:37:06 +0000145eight (this is intended to be the same rule as used by \UNIX{}). The
Fred Drakef6669171998-05-06 19:52:49 +0000146total number of spaces preceding the first non-blank character then
147determines the line's indentation. Indentation cannot be split over
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000148multiple physical lines using backslashes; the whitespace up to the
149first backslash determines the indentation.
150
151\strong{Cross-platform compatibility note:} because of the nature of
152text editors on non-UNIX platforms, it is unwise to use a mixture of
153spaces and tabs for the indentation in a single source file.
154
155A formfeed character may be present at the start of the line; it will
Fred Drakee15956b2000-04-03 04:51:13 +0000156be ignored for the indentation calculations above. Formfeed
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000157characters occurring elsewhere in the leading whitespace have an
158undefined effect (for instance, they may reset the space count to
159zero).
Fred Drakef6669171998-05-06 19:52:49 +0000160
161The indentation levels of consecutive lines are used to generate
162INDENT and DEDENT tokens, using a stack, as follows.
163\index{INDENT token}
164\index{DEDENT token}
165
166Before the first line of the file is read, a single zero is pushed on
167the stack; this will never be popped off again. The numbers pushed on
168the stack will always be strictly increasing from bottom to top. At
169the beginning of each logical line, the line's indentation level is
170compared to the top of the stack. If it is equal, nothing happens.
171If it is larger, it is pushed on the stack, and one INDENT token is
Fred Drake5c07d9b1998-05-14 19:37:06 +0000172generated. If it is smaller, it \emph{must} be one of the numbers
Fred Drakef6669171998-05-06 19:52:49 +0000173occurring on the stack; all numbers on the stack that are larger are
174popped off, and for each number popped off a DEDENT token is
175generated. At the end of the file, a DEDENT token is generated for
176each number remaining on the stack that is larger than zero.
177
178Here is an example of a correctly (though confusingly) indented piece
179of Python code:
180
181\begin{verbatim}
182def perm(l):
183 # Compute the list of all permutations of l
Fred Drakef6669171998-05-06 19:52:49 +0000184 if len(l) <= 1:
185 return [l]
186 r = []
187 for i in range(len(l)):
188 s = l[:i] + l[i+1:]
189 p = perm(s)
190 for x in p:
191 r.append(l[i:i+1] + x)
192 return r
193\end{verbatim}
194
195The following example shows various indentation errors:
196
197\begin{verbatim}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000198 def perm(l): # error: first line indented
Fred Drakef6669171998-05-06 19:52:49 +0000199 for i in range(len(l)): # error: not indented
200 s = l[:i] + l[i+1:]
201 p = perm(l[:i] + l[i+1:]) # error: unexpected indent
202 for x in p:
203 r.append(l[i:i+1] + x)
204 return r # error: inconsistent dedent
205\end{verbatim}
206
207(Actually, the first three errors are detected by the parser; only the
208last error is found by the lexical analyzer --- the indentation of
Fred Drake5c07d9b1998-05-14 19:37:06 +0000209\code{return r} does not match a level popped off the stack.)
Fred Drakef6669171998-05-06 19:52:49 +0000210
Fred Drakef5eae662001-06-23 05:26:52 +0000211
Fred Drake61c77281998-07-28 19:34:22 +0000212\subsection{Whitespace between tokens\label{whitespace}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000213
214Except at the beginning of a logical line or in string literals, the
215whitespace characters space, tab and formfeed can be used
216interchangeably to separate tokens. Whitespace is needed between two
217tokens only if their concatenation could otherwise be interpreted as a
218different token (e.g., ab is one token, but a b is two tokens).
219
Fred Drakef5eae662001-06-23 05:26:52 +0000220
Fred Drake61c77281998-07-28 19:34:22 +0000221\section{Other tokens\label{other-tokens}}
Fred Drakef6669171998-05-06 19:52:49 +0000222
223Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000224exist: \emph{identifiers}, \emph{keywords}, \emph{literals},
225\emph{operators}, and \emph{delimiters}.
226Whitespace characters (other than line terminators, discussed earlier)
227are not tokens, but serve to delimit tokens.
228Where
Fred Drakef6669171998-05-06 19:52:49 +0000229ambiguity exists, a token comprises the longest possible string that
230forms a legal token, when read from left to right.
231
Fred Drakef5eae662001-06-23 05:26:52 +0000232
Fred Drake61c77281998-07-28 19:34:22 +0000233\section{Identifiers and keywords\label{identifiers}}
Fred Drakef6669171998-05-06 19:52:49 +0000234
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000235Identifiers (also referred to as \emph{names}) are described by the following
Fred Drakef6669171998-05-06 19:52:49 +0000236lexical definitions:
237\index{identifier}
238\index{name}
239
240\begin{verbatim}
241identifier: (letter|"_") (letter|digit|"_")*
242letter: lowercase | uppercase
243lowercase: "a"..."z"
244uppercase: "A"..."Z"
245digit: "0"..."9"
246\end{verbatim}
247
248Identifiers are unlimited in length. Case is significant.
249
Fred Drakef5eae662001-06-23 05:26:52 +0000250
Fred Drake61c77281998-07-28 19:34:22 +0000251\subsection{Keywords\label{keywords}}
Fred Drakef6669171998-05-06 19:52:49 +0000252
Fred Drake5c07d9b1998-05-14 19:37:06 +0000253The following identifiers are used as reserved words, or
254\emph{keywords} of the language, and cannot be used as ordinary
255identifiers. They must be spelled exactly as written here:%
256\index{keyword}%
Fred Drakef6669171998-05-06 19:52:49 +0000257\index{reserved word}
258
259\begin{verbatim}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000260and del for is raise
261assert elif from lambda return
262break else global not try
Fred Drakef5eae662001-06-23 05:26:52 +0000263class except if or yeild
264continue exec import pass while
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000265def finally in print
Fred Drakef6669171998-05-06 19:52:49 +0000266\end{verbatim}
267
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000268% When adding keywords, use reswords.py for reformatting
269
Fred Drakef5eae662001-06-23 05:26:52 +0000270
Fred Drake61c77281998-07-28 19:34:22 +0000271\subsection{Reserved classes of identifiers\label{id-classes}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000272
273Certain classes of identifiers (besides keywords) have special
274meanings. These are:
275
Fred Drake39fc1bc1999-03-05 18:30:21 +0000276\begin{tableiii}{l|l|l}{code}{Form}{Meaning}{Notes}
277\lineiii{_*}{Not imported by \samp{from \var{module} import *}}{(1)}
278\lineiii{__*__}{System-defined name}{}
279\lineiii{__*}{Class-private name mangling}{}
280\end{tableiii}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000281
282(XXX need section references here.)
Fred Drakef6669171998-05-06 19:52:49 +0000283
Fred Drake39fc1bc1999-03-05 18:30:21 +0000284Note:
285
286\begin{description}
287\item[(1)] The special identifier \samp{_} is used in the interactive
288interpreter to store the result of the last evaluation; it is stored
289in the \module{__builtin__} module. When not in interactive mode,
290\samp{_} has no special meaning and is not defined.
291\end{description}
292
293
Fred Drake61c77281998-07-28 19:34:22 +0000294\section{Literals\label{literals}}
Fred Drakef6669171998-05-06 19:52:49 +0000295
296Literals are notations for constant values of some built-in types.
297\index{literal}
298\index{constant}
299
Fred Drakef5eae662001-06-23 05:26:52 +0000300
Fred Drake61c77281998-07-28 19:34:22 +0000301\subsection{String literals\label{strings}}
Fred Drakef6669171998-05-06 19:52:49 +0000302
303String literals are described by the following lexical definitions:
304\index{string literal}
305
306\begin{verbatim}
307stringliteral: shortstring | longstring
308shortstring: "'" shortstringitem* "'" | '"' shortstringitem* '"'
309longstring: "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
310shortstringitem: shortstringchar | escapeseq
311longstringitem: longstringchar | escapeseq
312shortstringchar: <any ASCII character except "\" or newline or the quote>
313longstringchar: <any ASCII character except "\">
314escapeseq: "\" <any ASCII character>
315\end{verbatim}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000316\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000317
Fred Drakedea764d2000-12-19 04:52:03 +0000318\index{triple-quoted string}
319\index{Unicode Consortium}
320\index{string!Unicode}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000321In plain English: String literals can be enclosed in matching single
322quotes (\code{'}) or double quotes (\code{"}). They can also be
323enclosed in matching groups of three single or double quotes (these
324are generally referred to as \emph{triple-quoted strings}). The
325backslash (\code{\e}) character is used to escape characters that
326otherwise have a special meaning, such as newline, backslash itself,
327or the quote character. String literals may optionally be prefixed
Fred Drakedea764d2000-12-19 04:52:03 +0000328with a letter `r' or `R'; such strings are called
329\dfn{raw strings}\index{raw string} and use different rules for
330backslash escape sequences. A prefix of 'u' or 'U' makes the string
331a Unicode string. Unicode strings use the Unicode character set as
332defined by the Unicode Consortium and ISO~10646. Some additional
333escape sequences, described below, are available in Unicode strings.
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000334
335In triple-quoted strings,
Fred Drakef6669171998-05-06 19:52:49 +0000336unescaped newlines and quotes are allowed (and are retained), except
337that three unescaped quotes in a row terminate the string. (A
338``quote'' is the character used to open the string, i.e. either
Fred Drake5c07d9b1998-05-14 19:37:06 +0000339\code{'} or \code{"}.)
Fred Drakef6669171998-05-06 19:52:49 +0000340
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000341Unless an `r' or `R' prefix is present, escape sequences in strings
342are interpreted according to rules similar
343to those used by Standard \C{}. The recognized escape sequences are:
Fred Drakef6669171998-05-06 19:52:49 +0000344\index{physical line}
345\index{escape sequence}
346\index{Standard C}
347\index{C}
348
Fred Drakea1cce711998-07-24 22:12:32 +0000349\begin{tableii}{l|l}{code}{Escape Sequence}{Meaning}
350\lineii{\e\var{newline}} {Ignored}
351\lineii{\e\e} {Backslash (\code{\e})}
352\lineii{\e'} {Single quote (\code{'})}
353\lineii{\e"} {Double quote (\code{"})}
354\lineii{\e a} {\ASCII{} Bell (BEL)}
355\lineii{\e b} {\ASCII{} Backspace (BS)}
356\lineii{\e f} {\ASCII{} Formfeed (FF)}
357\lineii{\e n} {\ASCII{} Linefeed (LF)}
Fred Drakedea764d2000-12-19 04:52:03 +0000358\lineii{\e N\{\var{name}\}}
359 {Character named \var{name} in the Unicode database (Unicode only)}
Fred Drakea1cce711998-07-24 22:12:32 +0000360\lineii{\e r} {\ASCII{} Carriage Return (CR)}
361\lineii{\e t} {\ASCII{} Horizontal Tab (TAB)}
Fred Drakedea764d2000-12-19 04:52:03 +0000362\lineii{\e u\var{xxxx}}
363 {Character with 16-bit hex value \var{xxxx} (Unicode only)}
364\lineii{\e U\var{xxxxxxxx}}
365 {Character with 32-bit hex value \var{xxxxxxxx} (Unicode only)}
Fred Drakea1cce711998-07-24 22:12:32 +0000366\lineii{\e v} {\ASCII{} Vertical Tab (VT)}
Fred Drakedea764d2000-12-19 04:52:03 +0000367\lineii{\e\var{ooo}} {\ASCII{} character with octal value \var{ooo}}
368\lineii{\e x\var{hh}} {\ASCII{} character with hex value \var{hh}}
Fred Drakea1cce711998-07-24 22:12:32 +0000369\end{tableii}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000370\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000371
Tim Peters75302082001-02-14 04:03:51 +0000372As in Standard C, up to three octal digits are accepted. However,
373exactly two hex digits are taken in hex escapes.
Fred Drakef6669171998-05-06 19:52:49 +0000374
Fred Drakedea764d2000-12-19 04:52:03 +0000375Unlike Standard \index{unrecognized escape sequence}C,
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000376all unrecognized escape sequences are left in the string unchanged,
Fred Drakedea764d2000-12-19 04:52:03 +0000377i.e., \emph{the backslash is left in the string}. (This behavior is
Fred Drakef6669171998-05-06 19:52:49 +0000378useful when debugging: if an escape sequence is mistyped, the
Fred Drakedea764d2000-12-19 04:52:03 +0000379resulting output is more easily recognized as broken.) It is also
380important to note that the escape sequences marked as ``(Unicode
381only)'' in the table above fall into the category of unrecognized
382escapes for non-Unicode string literals.
Fred Drakef6669171998-05-06 19:52:49 +0000383
Fred Drake347a6252001-01-09 21:38:16 +0000384When an `r' or `R' prefix is present, a character following a
385backslash is included in the string without change, and \emph{all
386backslashes are left in the string}. For example, the string literal
387\code{r"\e n"} consists of two characters: a backslash and a lowercase
388`n'. String quotes can be escaped with a backslash, but the backslash
389remains in the string; for example, \code{r"\e""} is a valid string
390literal consisting of two characters: a backslash and a double quote;
391\code{r"\e"} is not a value string literal (even a raw string cannot
392end in an odd number of backslashes). Specifically, \emph{a raw
393string cannot end in a single backslash} (since the backslash would
394escape the following quote character). Note also that a single
395backslash followed by a newline is interpreted as those two characters
396as part of the string, \emph{not} as a line continuation.
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000397
Fred Drakef5eae662001-06-23 05:26:52 +0000398
Fred Drake61c77281998-07-28 19:34:22 +0000399\subsection{String literal concatenation\label{string-catenation}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000400
401Multiple adjacent string literals (delimited by whitespace), possibly
402using different quoting conventions, are allowed, and their meaning is
403the same as their concatenation. Thus, \code{"hello" 'world'} is
404equivalent to \code{"helloworld"}. This feature can be used to reduce
405the number of backslashes needed, to split long strings conveniently
406across long lines, or even to add comments to parts of strings, for
407example:
408
409\begin{verbatim}
410re.compile("[A-Za-z_]" # letter or underscore
411 "[A-Za-z0-9_]*" # letter, digit or underscore
412 )
413\end{verbatim}
414
415Note that this feature is defined at the syntactical level, but
416implemented at compile time. The `+' operator must be used to
417concatenate string expressions at run time. Also note that literal
418concatenation can use different quoting styles for each component
419(even mixing raw strings and triple quoted strings).
420
Fred Drake2ed27d32000-11-17 19:05:12 +0000421
422\subsection{Unicode literals \label{unicode}}
423
424XXX explain more here...
425
426
Fred Drake61c77281998-07-28 19:34:22 +0000427\subsection{Numeric literals\label{numbers}}
Fred Drakef6669171998-05-06 19:52:49 +0000428
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000429There are four types of numeric literals: plain integers, long
430integers, floating point numbers, and imaginary numbers. There are no
431complex literals (complex numbers can be formed by adding a real
432number and an imaginary number).
Fred Drakef6669171998-05-06 19:52:49 +0000433\index{number}
434\index{numeric literal}
435\index{integer literal}
436\index{plain integer literal}
437\index{long integer literal}
438\index{floating point literal}
439\index{hexadecimal literal}
440\index{octal literal}
441\index{decimal literal}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000442\index{imaginary literal}
443\index{complex literal}
444
445Note that numeric literals do not include a sign; a phrase like
446\code{-1} is actually an expression composed of the unary operator
447`\code{-}' and the literal \code{1}.
448
Fred Drakef5eae662001-06-23 05:26:52 +0000449
Fred Drake61c77281998-07-28 19:34:22 +0000450\subsection{Integer and long integer literals\label{integers}}
Fred Drakef6669171998-05-06 19:52:49 +0000451
452Integer and long integer literals are described by the following
453lexical definitions:
454
455\begin{verbatim}
456longinteger: integer ("l"|"L")
457integer: decimalinteger | octinteger | hexinteger
458decimalinteger: nonzerodigit digit* | "0"
459octinteger: "0" octdigit+
460hexinteger: "0" ("x"|"X") hexdigit+
Fred Drakef6669171998-05-06 19:52:49 +0000461nonzerodigit: "1"..."9"
462octdigit: "0"..."7"
463hexdigit: digit|"a"..."f"|"A"..."F"
464\end{verbatim}
465
466Although both lower case `l' and upper case `L' are allowed as suffix
467for long integers, it is strongly recommended to always use `L', since
468the letter `l' looks too much like the digit `1'.
469
470Plain integer decimal literals must be at most 2147483647 (i.e., the
471largest positive integer, using 32-bit arithmetic). Plain octal and
472hexadecimal literals may be as large as 4294967295, but values larger
473than 2147483647 are converted to a negative value by subtracting
4744294967296. There is no limit for long integer literals apart from
475what can be stored in available memory.
476
477Some examples of plain and long integer literals:
478
479\begin{verbatim}
4807 2147483647 0177 0x80000000
4813L 79228162514264337593543950336L 0377L 0x100000000L
482\end{verbatim}
483
Fred Drakef5eae662001-06-23 05:26:52 +0000484
Fred Drake61c77281998-07-28 19:34:22 +0000485\subsection{Floating point literals\label{floating}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000486
Fred Drakef6669171998-05-06 19:52:49 +0000487Floating point literals are described by the following lexical
488definitions:
489
490\begin{verbatim}
491floatnumber: pointfloat | exponentfloat
492pointfloat: [intpart] fraction | intpart "."
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000493exponentfloat: (nonzerodigit digit* | pointfloat) exponent
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000494intpart: nonzerodigit digit* | "0"
Fred Drakef6669171998-05-06 19:52:49 +0000495fraction: "." digit+
496exponent: ("e"|"E") ["+"|"-"] digit+
497\end{verbatim}
498
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000499Note that the integer part of a floating point number cannot look like
Fred Drakee15956b2000-04-03 04:51:13 +0000500an octal integer, though the exponent may look like an octal literal
501but will always be interpreted using radix 10. For example,
502\samp{1e010} is legal, while \samp{07.1} is a syntax error.
Fred Drakef6669171998-05-06 19:52:49 +0000503The allowed range of floating point literals is
504implementation-dependent.
Fred Drakef6669171998-05-06 19:52:49 +0000505Some examples of floating point literals:
506
507\begin{verbatim}
5083.14 10. .001 1e100 3.14e-10
509\end{verbatim}
510
511Note that numeric literals do not include a sign; a phrase like
Fred Drake5c07d9b1998-05-14 19:37:06 +0000512\code{-1} is actually an expression composed of the operator
513\code{-} and the literal \code{1}.
Fred Drakef6669171998-05-06 19:52:49 +0000514
Fred Drakef5eae662001-06-23 05:26:52 +0000515
Fred Drake61c77281998-07-28 19:34:22 +0000516\subsection{Imaginary literals\label{imaginary}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000517
518Imaginary literals are described by the following lexical definitions:
519
520\begin{verbatim}
521imagnumber: (floatnumber | intpart) ("j"|"J")
522\end{verbatim}
523
Fred Drakee15956b2000-04-03 04:51:13 +0000524An imaginary literal yields a complex number with a real part of
Guido van Rossum60f2f0c1998-06-15 18:00:50 +00005250.0. Complex numbers are represented as a pair of floating point
526numbers and have the same restrictions on their range. To create a
527complex number with a nonzero real part, add a floating point number
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000528to it, e.g., \code{(3+4j)}. Some examples of imaginary literals:
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000529
530\begin{verbatim}
Guido van Rossum7c0240f1998-07-24 15:36:43 +00005313.14j 10.j 10j .001j 1e100j 3.14e-10j
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000532\end{verbatim}
533
534
Fred Drake61c77281998-07-28 19:34:22 +0000535\section{Operators\label{operators}}
Fred Drakef6669171998-05-06 19:52:49 +0000536
537The following tokens are operators:
538\index{operators}
539
540\begin{verbatim}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000541+ - * ** / %
Fred Drakef6669171998-05-06 19:52:49 +0000542<< >> & | ^ ~
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000543< > <= >= == != <>
Fred Drakef6669171998-05-06 19:52:49 +0000544\end{verbatim}
545
Fred Drake5c07d9b1998-05-14 19:37:06 +0000546The comparison operators \code{<>} and \code{!=} are alternate
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000547spellings of the same operator. \code{!=} is the preferred spelling;
548\code{<>} is obsolescent.
Fred Drakef6669171998-05-06 19:52:49 +0000549
Fred Drakef5eae662001-06-23 05:26:52 +0000550
Fred Drake61c77281998-07-28 19:34:22 +0000551\section{Delimiters\label{delimiters}}
Fred Drakef6669171998-05-06 19:52:49 +0000552
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000553The following tokens serve as delimiters in the grammar:
Fred Drakef6669171998-05-06 19:52:49 +0000554\index{delimiters}
555
556\begin{verbatim}
557( ) [ ] { }
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000558, : . ` = ;
Thomas Wouters12bba852000-08-24 20:06:04 +0000559+= -= *= /= %= **=
560&= |= ^= >>= <<=
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000561\end{verbatim}
562
563The period can also occur in floating-point and imaginary literals. A
Fred Drakee15956b2000-04-03 04:51:13 +0000564sequence of three periods has a special meaning as an ellipsis in slices.
Thomas Wouters12bba852000-08-24 20:06:04 +0000565The second half of the list, the augmented assignment operators, serve
566lexically as delimiters, but also perform an operation.
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000567
568The following printing ASCII characters have special meaning as part
569of other tokens or are otherwise significant to the lexical analyzer:
570
571\begin{verbatim}
572' " # \
Fred Drakef6669171998-05-06 19:52:49 +0000573\end{verbatim}
574
575The following printing \ASCII{} characters are not used in Python. Their
576occurrence outside string literals and comments is an unconditional
577error:
Fred Drake5c07d9b1998-05-14 19:37:06 +0000578\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000579
580\begin{verbatim}
581@ $ ?
582\end{verbatim}