blob: 8a44b03c3d0e50bc3c320ceb67580e7ee71650dc [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
Fred Drakecb4638a2001-07-06 22:49:53 +0000240\begin{productionlist}
241 \production{identifier}
242 {(\token{letter}|"_") (\token{letter} | \token{digit} | "_")*}
243 \production{letter}
244 {\token{lowercase} | \token{uppercase}}
245 \production{lowercase}
246 {"a"..."z"}
247 \production{uppercase}
248 {"A"..."Z"}
249 \production{digit}
250 {"0"..."9"}
251\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000252
253Identifiers are unlimited in length. Case is significant.
254
Fred Drakef5eae662001-06-23 05:26:52 +0000255
Fred Drake61c77281998-07-28 19:34:22 +0000256\subsection{Keywords\label{keywords}}
Fred Drakef6669171998-05-06 19:52:49 +0000257
Fred Drake5c07d9b1998-05-14 19:37:06 +0000258The following identifiers are used as reserved words, or
259\emph{keywords} of the language, and cannot be used as ordinary
260identifiers. They must be spelled exactly as written here:%
261\index{keyword}%
Fred Drakef6669171998-05-06 19:52:49 +0000262\index{reserved word}
263
264\begin{verbatim}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000265and del for is raise
266assert elif from lambda return
267break else global not try
Andrew M. Kuchlinge7e03cd2001-06-23 16:26:44 +0000268class except if or yield
Fred Drakef5eae662001-06-23 05:26:52 +0000269continue exec import pass while
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000270def finally in print
Fred Drakef6669171998-05-06 19:52:49 +0000271\end{verbatim}
272
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000273% When adding keywords, use reswords.py for reformatting
274
Fred Drakef5eae662001-06-23 05:26:52 +0000275
Fred Drake61c77281998-07-28 19:34:22 +0000276\subsection{Reserved classes of identifiers\label{id-classes}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000277
278Certain classes of identifiers (besides keywords) have special
279meanings. These are:
280
Fred Drake39fc1bc1999-03-05 18:30:21 +0000281\begin{tableiii}{l|l|l}{code}{Form}{Meaning}{Notes}
282\lineiii{_*}{Not imported by \samp{from \var{module} import *}}{(1)}
283\lineiii{__*__}{System-defined name}{}
284\lineiii{__*}{Class-private name mangling}{}
285\end{tableiii}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000286
287(XXX need section references here.)
Fred Drakef6669171998-05-06 19:52:49 +0000288
Fred Drake39fc1bc1999-03-05 18:30:21 +0000289Note:
290
291\begin{description}
292\item[(1)] The special identifier \samp{_} is used in the interactive
293interpreter to store the result of the last evaluation; it is stored
294in the \module{__builtin__} module. When not in interactive mode,
295\samp{_} has no special meaning and is not defined.
296\end{description}
297
298
Fred Drake61c77281998-07-28 19:34:22 +0000299\section{Literals\label{literals}}
Fred Drakef6669171998-05-06 19:52:49 +0000300
301Literals are notations for constant values of some built-in types.
302\index{literal}
303\index{constant}
304
Fred Drakef5eae662001-06-23 05:26:52 +0000305
Fred Drake61c77281998-07-28 19:34:22 +0000306\subsection{String literals\label{strings}}
Fred Drakef6669171998-05-06 19:52:49 +0000307
308String literals are described by the following lexical definitions:
309\index{string literal}
310
Fred Drake5c07d9b1998-05-14 19:37:06 +0000311\index{ASCII@\ASCII{}}
Fred Drakecb4638a2001-07-06 22:49:53 +0000312\begin{productionlist}
313 \production{stringliteral}
314 {\token{shortstring} | \token{longstring}}
315 \production{shortstring}
316 {"'" \token{shortstringitem}* "'"
317 | '"' \token{shortstringitem}* '"'}
318 \production{longstring}
319 {"'''" \token{longstringitem}* "'''"
320 | '"""' \token{longstringitem}* '"""'}
321 \production{shortstringitem}
322 {\token{shortstringchar} | \token{escapeseq}}
323 \production{longstringitem}
324 {\token{longstringchar} | \token{escapeseq}}
325 \production{shortstringchar}
326 {<any ASCII character except "\e" or newline or the quote>}
327 \production{longstringchar}
328 {<any ASCII character except "\e">}
329 \production{escapeseq}
330 {"\e" <any ASCII character>}
331\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000332
Fred Drakedea764d2000-12-19 04:52:03 +0000333\index{triple-quoted string}
334\index{Unicode Consortium}
335\index{string!Unicode}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000336In plain English: String literals can be enclosed in matching single
337quotes (\code{'}) or double quotes (\code{"}). They can also be
338enclosed in matching groups of three single or double quotes (these
339are generally referred to as \emph{triple-quoted strings}). The
340backslash (\code{\e}) character is used to escape characters that
341otherwise have a special meaning, such as newline, backslash itself,
342or the quote character. String literals may optionally be prefixed
Fred Drakedea764d2000-12-19 04:52:03 +0000343with a letter `r' or `R'; such strings are called
344\dfn{raw strings}\index{raw string} and use different rules for
345backslash escape sequences. A prefix of 'u' or 'U' makes the string
346a Unicode string. Unicode strings use the Unicode character set as
347defined by the Unicode Consortium and ISO~10646. Some additional
348escape sequences, described below, are available in Unicode strings.
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000349
350In triple-quoted strings,
Fred Drakef6669171998-05-06 19:52:49 +0000351unescaped newlines and quotes are allowed (and are retained), except
352that three unescaped quotes in a row terminate the string. (A
353``quote'' is the character used to open the string, i.e. either
Fred Drake5c07d9b1998-05-14 19:37:06 +0000354\code{'} or \code{"}.)
Fred Drakef6669171998-05-06 19:52:49 +0000355
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000356Unless an `r' or `R' prefix is present, escape sequences in strings
357are interpreted according to rules similar
358to those used by Standard \C{}. The recognized escape sequences are:
Fred Drakef6669171998-05-06 19:52:49 +0000359\index{physical line}
360\index{escape sequence}
361\index{Standard C}
362\index{C}
363
Fred Drakea1cce711998-07-24 22:12:32 +0000364\begin{tableii}{l|l}{code}{Escape Sequence}{Meaning}
365\lineii{\e\var{newline}} {Ignored}
366\lineii{\e\e} {Backslash (\code{\e})}
367\lineii{\e'} {Single quote (\code{'})}
368\lineii{\e"} {Double quote (\code{"})}
369\lineii{\e a} {\ASCII{} Bell (BEL)}
370\lineii{\e b} {\ASCII{} Backspace (BS)}
371\lineii{\e f} {\ASCII{} Formfeed (FF)}
372\lineii{\e n} {\ASCII{} Linefeed (LF)}
Fred Drakedea764d2000-12-19 04:52:03 +0000373\lineii{\e N\{\var{name}\}}
374 {Character named \var{name} in the Unicode database (Unicode only)}
Fred Drakea1cce711998-07-24 22:12:32 +0000375\lineii{\e r} {\ASCII{} Carriage Return (CR)}
376\lineii{\e t} {\ASCII{} Horizontal Tab (TAB)}
Fred Drakedea764d2000-12-19 04:52:03 +0000377\lineii{\e u\var{xxxx}}
378 {Character with 16-bit hex value \var{xxxx} (Unicode only)}
379\lineii{\e U\var{xxxxxxxx}}
380 {Character with 32-bit hex value \var{xxxxxxxx} (Unicode only)}
Fred Drakea1cce711998-07-24 22:12:32 +0000381\lineii{\e v} {\ASCII{} Vertical Tab (VT)}
Fred Drakedea764d2000-12-19 04:52:03 +0000382\lineii{\e\var{ooo}} {\ASCII{} character with octal value \var{ooo}}
383\lineii{\e x\var{hh}} {\ASCII{} character with hex value \var{hh}}
Fred Drakea1cce711998-07-24 22:12:32 +0000384\end{tableii}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000385\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000386
Tim Peters75302082001-02-14 04:03:51 +0000387As in Standard C, up to three octal digits are accepted. However,
388exactly two hex digits are taken in hex escapes.
Fred Drakef6669171998-05-06 19:52:49 +0000389
Fred Drakedea764d2000-12-19 04:52:03 +0000390Unlike Standard \index{unrecognized escape sequence}C,
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000391all unrecognized escape sequences are left in the string unchanged,
Fred Drakedea764d2000-12-19 04:52:03 +0000392i.e., \emph{the backslash is left in the string}. (This behavior is
Fred Drakef6669171998-05-06 19:52:49 +0000393useful when debugging: if an escape sequence is mistyped, the
Fred Drakedea764d2000-12-19 04:52:03 +0000394resulting output is more easily recognized as broken.) It is also
395important to note that the escape sequences marked as ``(Unicode
396only)'' in the table above fall into the category of unrecognized
397escapes for non-Unicode string literals.
Fred Drakef6669171998-05-06 19:52:49 +0000398
Fred Drake347a6252001-01-09 21:38:16 +0000399When an `r' or `R' prefix is present, a character following a
400backslash is included in the string without change, and \emph{all
401backslashes are left in the string}. For example, the string literal
402\code{r"\e n"} consists of two characters: a backslash and a lowercase
403`n'. String quotes can be escaped with a backslash, but the backslash
404remains in the string; for example, \code{r"\e""} is a valid string
405literal consisting of two characters: a backslash and a double quote;
406\code{r"\e"} is not a value string literal (even a raw string cannot
407end in an odd number of backslashes). Specifically, \emph{a raw
408string cannot end in a single backslash} (since the backslash would
409escape the following quote character). Note also that a single
410backslash followed by a newline is interpreted as those two characters
411as part of the string, \emph{not} as a line continuation.
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000412
Fred Drakef5eae662001-06-23 05:26:52 +0000413
Fred Drake61c77281998-07-28 19:34:22 +0000414\subsection{String literal concatenation\label{string-catenation}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000415
416Multiple adjacent string literals (delimited by whitespace), possibly
417using different quoting conventions, are allowed, and their meaning is
418the same as their concatenation. Thus, \code{"hello" 'world'} is
419equivalent to \code{"helloworld"}. This feature can be used to reduce
420the number of backslashes needed, to split long strings conveniently
421across long lines, or even to add comments to parts of strings, for
422example:
423
424\begin{verbatim}
425re.compile("[A-Za-z_]" # letter or underscore
426 "[A-Za-z0-9_]*" # letter, digit or underscore
427 )
428\end{verbatim}
429
430Note that this feature is defined at the syntactical level, but
431implemented at compile time. The `+' operator must be used to
432concatenate string expressions at run time. Also note that literal
433concatenation can use different quoting styles for each component
434(even mixing raw strings and triple quoted strings).
435
Fred Drake2ed27d32000-11-17 19:05:12 +0000436
437\subsection{Unicode literals \label{unicode}}
438
439XXX explain more here...
440
441
Fred Drake61c77281998-07-28 19:34:22 +0000442\subsection{Numeric literals\label{numbers}}
Fred Drakef6669171998-05-06 19:52:49 +0000443
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000444There are four types of numeric literals: plain integers, long
445integers, floating point numbers, and imaginary numbers. There are no
446complex literals (complex numbers can be formed by adding a real
447number and an imaginary number).
Fred Drakef6669171998-05-06 19:52:49 +0000448\index{number}
449\index{numeric literal}
450\index{integer literal}
451\index{plain integer literal}
452\index{long integer literal}
453\index{floating point literal}
454\index{hexadecimal literal}
455\index{octal literal}
456\index{decimal literal}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000457\index{imaginary literal}
458\index{complex literal}
459
460Note that numeric literals do not include a sign; a phrase like
461\code{-1} is actually an expression composed of the unary operator
462`\code{-}' and the literal \code{1}.
463
Fred Drakef5eae662001-06-23 05:26:52 +0000464
Fred Drake61c77281998-07-28 19:34:22 +0000465\subsection{Integer and long integer literals\label{integers}}
Fred Drakef6669171998-05-06 19:52:49 +0000466
467Integer and long integer literals are described by the following
468lexical definitions:
469
Fred Drakecb4638a2001-07-06 22:49:53 +0000470\begin{productionlist}
471 \production{longinteger}
472 {\token{integer} ("l" | "L")}
473 \production{integer}
474 {\token{decimalinteger} | \token{octinteger} | \token{hexinteger}}
475 \production{decimalinteger}
476 {\token{nonzerodigit} \token{digit}* | "0"}
477 \production{octinteger}
478 {"0" \token{octdigit}+}
479 \production{hexinteger}
480 {"0" ("x" | "X") \token{hexdigit}+}
481 \production{nonzerodigit}
482 {"1"..."9"}
483 \production{octdigit}
484 {"0"..."7"}
485 \production{hexdigit}
486 {\token{digit} | "a"..."f" | "A"..."F"}
487\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000488
489Although both lower case `l' and upper case `L' are allowed as suffix
490for long integers, it is strongly recommended to always use `L', since
491the letter `l' looks too much like the digit `1'.
492
493Plain integer decimal literals must be at most 2147483647 (i.e., the
494largest positive integer, using 32-bit arithmetic). Plain octal and
495hexadecimal literals may be as large as 4294967295, but values larger
496than 2147483647 are converted to a negative value by subtracting
4974294967296. There is no limit for long integer literals apart from
498what can be stored in available memory.
499
500Some examples of plain and long integer literals:
501
502\begin{verbatim}
5037 2147483647 0177 0x80000000
5043L 79228162514264337593543950336L 0377L 0x100000000L
505\end{verbatim}
506
Fred Drakef5eae662001-06-23 05:26:52 +0000507
Fred Drake61c77281998-07-28 19:34:22 +0000508\subsection{Floating point literals\label{floating}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000509
Fred Drakef6669171998-05-06 19:52:49 +0000510Floating point literals are described by the following lexical
511definitions:
512
Fred Drakecb4638a2001-07-06 22:49:53 +0000513\begin{productionlist}
514 \production{floatnumber}
515 {\token{pointfloat} | \token{exponentfloat}}
516 \production{pointfloat}
517 {[\token{intpart}] \token{fraction} | \token{intpart} "."}
518 \production{exponentfloat}
519 {(\token{nonzerodigit} \token{digit}* | \token{pointfloat})
520 \token{exponent}}
521 \production{intpart}
522 {\token{nonzerodigit} \token{digit}* | "0"}
523 \production{fraction}
524 {"." \token{digit}+}
525 \production{exponent}
526 {("e" | "E") ["+" | "-"] \token{digit}+}
527\end{productionlist}
Fred Drakef6669171998-05-06 19:52:49 +0000528
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000529Note that the integer part of a floating point number cannot look like
Fred Drakee15956b2000-04-03 04:51:13 +0000530an octal integer, though the exponent may look like an octal literal
531but will always be interpreted using radix 10. For example,
532\samp{1e010} is legal, while \samp{07.1} is a syntax error.
Fred Drakef6669171998-05-06 19:52:49 +0000533The allowed range of floating point literals is
534implementation-dependent.
Fred Drakef6669171998-05-06 19:52:49 +0000535Some examples of floating point literals:
536
537\begin{verbatim}
5383.14 10. .001 1e100 3.14e-10
539\end{verbatim}
540
541Note that numeric literals do not include a sign; a phrase like
Fred Drake5c07d9b1998-05-14 19:37:06 +0000542\code{-1} is actually an expression composed of the operator
543\code{-} and the literal \code{1}.
Fred Drakef6669171998-05-06 19:52:49 +0000544
Fred Drakef5eae662001-06-23 05:26:52 +0000545
Fred Drake61c77281998-07-28 19:34:22 +0000546\subsection{Imaginary literals\label{imaginary}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000547
548Imaginary literals are described by the following lexical definitions:
549
Fred Drakecb4638a2001-07-06 22:49:53 +0000550\begin{productionlist}
551 \production{imagnumber}{(\token{floatnumber} | \token{intpart}) ("j" | "J")}
552\end{productionlist}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000553
Fred Drakee15956b2000-04-03 04:51:13 +0000554An imaginary literal yields a complex number with a real part of
Guido van Rossum60f2f0c1998-06-15 18:00:50 +00005550.0. Complex numbers are represented as a pair of floating point
556numbers and have the same restrictions on their range. To create a
557complex number with a nonzero real part, add a floating point number
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000558to it, e.g., \code{(3+4j)}. Some examples of imaginary literals:
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000559
560\begin{verbatim}
Guido van Rossum7c0240f1998-07-24 15:36:43 +00005613.14j 10.j 10j .001j 1e100j 3.14e-10j
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000562\end{verbatim}
563
564
Fred Drake61c77281998-07-28 19:34:22 +0000565\section{Operators\label{operators}}
Fred Drakef6669171998-05-06 19:52:49 +0000566
567The following tokens are operators:
568\index{operators}
569
570\begin{verbatim}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000571+ - * ** / %
Fred Drakef6669171998-05-06 19:52:49 +0000572<< >> & | ^ ~
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000573< > <= >= == != <>
Fred Drakef6669171998-05-06 19:52:49 +0000574\end{verbatim}
575
Fred Drake5c07d9b1998-05-14 19:37:06 +0000576The comparison operators \code{<>} and \code{!=} are alternate
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000577spellings of the same operator. \code{!=} is the preferred spelling;
578\code{<>} is obsolescent.
Fred Drakef6669171998-05-06 19:52:49 +0000579
Fred Drakef5eae662001-06-23 05:26:52 +0000580
Fred Drake61c77281998-07-28 19:34:22 +0000581\section{Delimiters\label{delimiters}}
Fred Drakef6669171998-05-06 19:52:49 +0000582
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000583The following tokens serve as delimiters in the grammar:
Fred Drakef6669171998-05-06 19:52:49 +0000584\index{delimiters}
585
586\begin{verbatim}
587( ) [ ] { }
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000588, : . ` = ;
Thomas Wouters12bba852000-08-24 20:06:04 +0000589+= -= *= /= %= **=
590&= |= ^= >>= <<=
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000591\end{verbatim}
592
593The period can also occur in floating-point and imaginary literals. A
Fred Drakee15956b2000-04-03 04:51:13 +0000594sequence of three periods has a special meaning as an ellipsis in slices.
Thomas Wouters12bba852000-08-24 20:06:04 +0000595The second half of the list, the augmented assignment operators, serve
596lexically as delimiters, but also perform an operation.
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000597
598The following printing ASCII characters have special meaning as part
599of other tokens or are otherwise significant to the lexical analyzer:
600
601\begin{verbatim}
602' " # \
Fred Drakef6669171998-05-06 19:52:49 +0000603\end{verbatim}
604
605The following printing \ASCII{} characters are not used in Python. Their
606occurrence outside string literals and comments is an unconditional
607error:
Fred Drake5c07d9b1998-05-14 19:37:06 +0000608\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000609
610\begin{verbatim}
611@ $ ?
612\end{verbatim}