blob: 64853fb9bf3f6f60b3be01ed16a41f474d490b63 [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 Drake61c77281998-07-28 19:34:22 +000031\section{Line structure\label{line-structure}}
Fred Drakef6669171998-05-06 19:52:49 +000032
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000033A Python program is divided into a number of \emph{logical lines}.
34\index{line structure}
35
Fred Drake61c77281998-07-28 19:34:22 +000036\subsection{Logical lines\label{logical}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000037
38The end of
Fred Drakef6669171998-05-06 19:52:49 +000039a logical line is represented by the token NEWLINE. Statements cannot
40cross logical line boundaries except where NEWLINE is allowed by the
Guido van Rossum7c0240f1998-07-24 15:36:43 +000041syntax (e.g., between statements in compound statements).
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000042A logical line is constructed from one or more \emph{physical lines}
43by following the explicit or implicit \emph{line joining} rules.
Fred Drakef6669171998-05-06 19:52:49 +000044\index{logical line}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000045\index{physical line}
46\index{line joining}
Fred Drakef6669171998-05-06 19:52:49 +000047\index{NEWLINE token}
48
Fred Drake61c77281998-07-28 19:34:22 +000049\subsection{Physical lines\label{physical}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000050
51A physical line ends in whatever the current platform's convention is
52for terminating lines. On \UNIX{}, this is the \ASCII{} LF (linefeed)
53character. On DOS/Windows, it is the \ASCII{} sequence CR LF (return
54followed by linefeed). On Macintosh, it is the \ASCII{} CR (return)
55character.
56
Fred Drake61c77281998-07-28 19:34:22 +000057\subsection{Comments\label{comments}}
Fred Drakef6669171998-05-06 19:52:49 +000058
Fred Drake5c07d9b1998-05-14 19:37:06 +000059A comment starts with a hash character (\code{\#}) that is not part of
Fred Drakef6669171998-05-06 19:52:49 +000060a string literal, and ends at the end of the physical line. A comment
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000061signifies the end of the logical line unless the implicit line joining
62rules are invoked.
63Comments are ignored by the syntax; they are not tokens.
Fred Drakef6669171998-05-06 19:52:49 +000064\index{comment}
Fred Drakef6669171998-05-06 19:52:49 +000065\index{hash character}
66
Fred Drake61c77281998-07-28 19:34:22 +000067\subsection{Explicit line joining\label{explicit-joining}}
Fred Drakef6669171998-05-06 19:52:49 +000068
69Two or more physical lines may be joined into logical lines using
Fred Drake5c07d9b1998-05-14 19:37:06 +000070backslash characters (\code{\e}), as follows: when a physical line ends
Fred Drakef6669171998-05-06 19:52:49 +000071in a backslash that is not part of a string literal or comment, it is
72joined with the following forming a single logical line, deleting the
73backslash and the following end-of-line character. For example:
74\index{physical line}
75\index{line joining}
76\index{line continuation}
77\index{backslash character}
78%
79\begin{verbatim}
80if 1900 < year < 2100 and 1 <= month <= 12 \
81 and 1 <= day <= 31 and 0 <= hour < 24 \
82 and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date
83 return 1
84\end{verbatim}
85
Guido van Rossum60f2f0c1998-06-15 18:00:50 +000086A line ending in a backslash cannot carry a comment. A backslash does
87not continue a comment. A backslash does not continue a token except
88for string literals (i.e., tokens other than string literals cannot be
89split across physical lines using a backslash). A backslash is
90illegal elsewhere on a line outside a string literal.
Fred Drakef6669171998-05-06 19:52:49 +000091
Fred Drakec411fa61999-02-22 14:32:18 +000092
Fred Drake61c77281998-07-28 19:34:22 +000093\subsection{Implicit line joining\label{implicit-joining}}
Fred Drakef6669171998-05-06 19:52:49 +000094
95Expressions in parentheses, square brackets or curly braces can be
96split over more than one physical line without using backslashes.
97For example:
98
99\begin{verbatim}
100month_names = ['Januari', 'Februari', 'Maart', # These are the
101 'April', 'Mei', 'Juni', # Dutch names
102 'Juli', 'Augustus', 'September', # for the months
103 'Oktober', 'November', 'December'] # of the year
104\end{verbatim}
105
106Implicitly continued lines can carry comments. The indentation of the
107continuation lines is not important. Blank continuation lines are
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000108allowed. There is no NEWLINE token between implicit continuation
109lines. Implicitly continued lines can also occur within triple-quoted
110strings (see below); in that case they cannot carry comments.
Fred Drakef6669171998-05-06 19:52:49 +0000111
Fred Drakef6669171998-05-06 19:52:49 +0000112
Fred Drakec411fa61999-02-22 14:32:18 +0000113\subsection{Blank lines \index{blank line}\label{blank-lines}}
114
115A logical line that contains only spaces, tabs, formfeeds and possibly
116a comment, is ignored (i.e., no NEWLINE token is generated). During
117interactive input of statements, handling of a blank line may differ
118depending on the implementation of the read-eval-print loop. In the
119standard implementation, an entirely blank logical line (i.e.\ one
120containing not even whitespace or a comment) terminates a multi-line
121statement.
122
Fred Drakef6669171998-05-06 19:52:49 +0000123
Fred Drake61c77281998-07-28 19:34:22 +0000124\subsection{Indentation\label{indentation}}
Fred Drakef6669171998-05-06 19:52:49 +0000125
126Leading whitespace (spaces and tabs) at the beginning of a logical
127line is used to compute the indentation level of the line, which in
128turn is used to determine the grouping of statements.
129\index{indentation}
130\index{whitespace}
131\index{leading whitespace}
132\index{space}
133\index{tab}
134\index{grouping}
135\index{statement grouping}
136
137First, tabs are replaced (from left to right) by one to eight spaces
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000138such that the total number of characters up to and including the
139replacement is a multiple of
Fred Drake5c07d9b1998-05-14 19:37:06 +0000140eight (this is intended to be the same rule as used by \UNIX{}). The
Fred Drakef6669171998-05-06 19:52:49 +0000141total number of spaces preceding the first non-blank character then
142determines the line's indentation. Indentation cannot be split over
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000143multiple physical lines using backslashes; the whitespace up to the
144first backslash determines the indentation.
145
146\strong{Cross-platform compatibility note:} because of the nature of
147text editors on non-UNIX platforms, it is unwise to use a mixture of
148spaces and tabs for the indentation in a single source file.
149
150A formfeed character may be present at the start of the line; it will
Fred Drakee15956b2000-04-03 04:51:13 +0000151be ignored for the indentation calculations above. Formfeed
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000152characters occurring elsewhere in the leading whitespace have an
153undefined effect (for instance, they may reset the space count to
154zero).
Fred Drakef6669171998-05-06 19:52:49 +0000155
156The indentation levels of consecutive lines are used to generate
157INDENT and DEDENT tokens, using a stack, as follows.
158\index{INDENT token}
159\index{DEDENT token}
160
161Before the first line of the file is read, a single zero is pushed on
162the stack; this will never be popped off again. The numbers pushed on
163the stack will always be strictly increasing from bottom to top. At
164the beginning of each logical line, the line's indentation level is
165compared to the top of the stack. If it is equal, nothing happens.
166If it is larger, it is pushed on the stack, and one INDENT token is
Fred Drake5c07d9b1998-05-14 19:37:06 +0000167generated. If it is smaller, it \emph{must} be one of the numbers
Fred Drakef6669171998-05-06 19:52:49 +0000168occurring on the stack; all numbers on the stack that are larger are
169popped off, and for each number popped off a DEDENT token is
170generated. At the end of the file, a DEDENT token is generated for
171each number remaining on the stack that is larger than zero.
172
173Here is an example of a correctly (though confusingly) indented piece
174of Python code:
175
176\begin{verbatim}
177def perm(l):
178 # Compute the list of all permutations of l
Fred Drakef6669171998-05-06 19:52:49 +0000179 if len(l) <= 1:
180 return [l]
181 r = []
182 for i in range(len(l)):
183 s = l[:i] + l[i+1:]
184 p = perm(s)
185 for x in p:
186 r.append(l[i:i+1] + x)
187 return r
188\end{verbatim}
189
190The following example shows various indentation errors:
191
192\begin{verbatim}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000193 def perm(l): # error: first line indented
Fred Drakef6669171998-05-06 19:52:49 +0000194 for i in range(len(l)): # error: not indented
195 s = l[:i] + l[i+1:]
196 p = perm(l[:i] + l[i+1:]) # error: unexpected indent
197 for x in p:
198 r.append(l[i:i+1] + x)
199 return r # error: inconsistent dedent
200\end{verbatim}
201
202(Actually, the first three errors are detected by the parser; only the
203last error is found by the lexical analyzer --- the indentation of
Fred Drake5c07d9b1998-05-14 19:37:06 +0000204\code{return r} does not match a level popped off the stack.)
Fred Drakef6669171998-05-06 19:52:49 +0000205
Fred Drake61c77281998-07-28 19:34:22 +0000206\subsection{Whitespace between tokens\label{whitespace}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000207
208Except at the beginning of a logical line or in string literals, the
209whitespace characters space, tab and formfeed can be used
210interchangeably to separate tokens. Whitespace is needed between two
211tokens only if their concatenation could otherwise be interpreted as a
212different token (e.g., ab is one token, but a b is two tokens).
213
Fred Drake61c77281998-07-28 19:34:22 +0000214\section{Other tokens\label{other-tokens}}
Fred Drakef6669171998-05-06 19:52:49 +0000215
216Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000217exist: \emph{identifiers}, \emph{keywords}, \emph{literals},
218\emph{operators}, and \emph{delimiters}.
219Whitespace characters (other than line terminators, discussed earlier)
220are not tokens, but serve to delimit tokens.
221Where
Fred Drakef6669171998-05-06 19:52:49 +0000222ambiguity exists, a token comprises the longest possible string that
223forms a legal token, when read from left to right.
224
Fred Drake61c77281998-07-28 19:34:22 +0000225\section{Identifiers and keywords\label{identifiers}}
Fred Drakef6669171998-05-06 19:52:49 +0000226
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000227Identifiers (also referred to as \emph{names}) are described by the following
Fred Drakef6669171998-05-06 19:52:49 +0000228lexical definitions:
229\index{identifier}
230\index{name}
231
232\begin{verbatim}
233identifier: (letter|"_") (letter|digit|"_")*
234letter: lowercase | uppercase
235lowercase: "a"..."z"
236uppercase: "A"..."Z"
237digit: "0"..."9"
238\end{verbatim}
239
240Identifiers are unlimited in length. Case is significant.
241
Fred Drake61c77281998-07-28 19:34:22 +0000242\subsection{Keywords\label{keywords}}
Fred Drakef6669171998-05-06 19:52:49 +0000243
Fred Drake5c07d9b1998-05-14 19:37:06 +0000244The following identifiers are used as reserved words, or
245\emph{keywords} of the language, and cannot be used as ordinary
246identifiers. They must be spelled exactly as written here:%
247\index{keyword}%
Fred Drakef6669171998-05-06 19:52:49 +0000248\index{reserved word}
249
250\begin{verbatim}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000251and del for is raise
252assert elif from lambda return
253break else global not try
254class except if or while
255continue exec import pass
256def finally in print
Fred Drakef6669171998-05-06 19:52:49 +0000257\end{verbatim}
258
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000259% When adding keywords, use reswords.py for reformatting
260
Fred Drake61c77281998-07-28 19:34:22 +0000261\subsection{Reserved classes of identifiers\label{id-classes}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000262
263Certain classes of identifiers (besides keywords) have special
264meanings. These are:
265
Fred Drake39fc1bc1999-03-05 18:30:21 +0000266\begin{tableiii}{l|l|l}{code}{Form}{Meaning}{Notes}
267\lineiii{_*}{Not imported by \samp{from \var{module} import *}}{(1)}
268\lineiii{__*__}{System-defined name}{}
269\lineiii{__*}{Class-private name mangling}{}
270\end{tableiii}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000271
272(XXX need section references here.)
Fred Drakef6669171998-05-06 19:52:49 +0000273
Fred Drake39fc1bc1999-03-05 18:30:21 +0000274Note:
275
276\begin{description}
277\item[(1)] The special identifier \samp{_} is used in the interactive
278interpreter to store the result of the last evaluation; it is stored
279in the \module{__builtin__} module. When not in interactive mode,
280\samp{_} has no special meaning and is not defined.
281\end{description}
282
283
Fred Drake61c77281998-07-28 19:34:22 +0000284\section{Literals\label{literals}}
Fred Drakef6669171998-05-06 19:52:49 +0000285
286Literals are notations for constant values of some built-in types.
287\index{literal}
288\index{constant}
289
Fred Drake61c77281998-07-28 19:34:22 +0000290\subsection{String literals\label{strings}}
Fred Drakef6669171998-05-06 19:52:49 +0000291
292String literals are described by the following lexical definitions:
293\index{string literal}
294
295\begin{verbatim}
296stringliteral: shortstring | longstring
297shortstring: "'" shortstringitem* "'" | '"' shortstringitem* '"'
298longstring: "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
299shortstringitem: shortstringchar | escapeseq
300longstringitem: longstringchar | escapeseq
301shortstringchar: <any ASCII character except "\" or newline or the quote>
302longstringchar: <any ASCII character except "\">
303escapeseq: "\" <any ASCII character>
304\end{verbatim}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000305\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000306
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000307In plain English: String literals can be enclosed in matching single
308quotes (\code{'}) or double quotes (\code{"}). They can also be
309enclosed in matching groups of three single or double quotes (these
310are generally referred to as \emph{triple-quoted strings}). The
311backslash (\code{\e}) character is used to escape characters that
312otherwise have a special meaning, such as newline, backslash itself,
313or the quote character. String literals may optionally be prefixed
314with a letter `r' or `R'; such strings are called raw strings and use
315different rules for backslash escape sequences.
316\index{triple-quoted string}
317\index{raw string}
318
319In triple-quoted strings,
Fred Drakef6669171998-05-06 19:52:49 +0000320unescaped newlines and quotes are allowed (and are retained), except
321that three unescaped quotes in a row terminate the string. (A
322``quote'' is the character used to open the string, i.e. either
Fred Drake5c07d9b1998-05-14 19:37:06 +0000323\code{'} or \code{"}.)
Fred Drakef6669171998-05-06 19:52:49 +0000324
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000325Unless an `r' or `R' prefix is present, escape sequences in strings
326are interpreted according to rules similar
327to those used by Standard \C{}. The recognized escape sequences are:
Fred Drakef6669171998-05-06 19:52:49 +0000328\index{physical line}
329\index{escape sequence}
330\index{Standard C}
331\index{C}
332
Fred Drakea1cce711998-07-24 22:12:32 +0000333\begin{tableii}{l|l}{code}{Escape Sequence}{Meaning}
334\lineii{\e\var{newline}} {Ignored}
335\lineii{\e\e} {Backslash (\code{\e})}
336\lineii{\e'} {Single quote (\code{'})}
337\lineii{\e"} {Double quote (\code{"})}
338\lineii{\e a} {\ASCII{} Bell (BEL)}
339\lineii{\e b} {\ASCII{} Backspace (BS)}
340\lineii{\e f} {\ASCII{} Formfeed (FF)}
341\lineii{\e n} {\ASCII{} Linefeed (LF)}
342\lineii{\e r} {\ASCII{} Carriage Return (CR)}
343\lineii{\e t} {\ASCII{} Horizontal Tab (TAB)}
344\lineii{\e v} {\ASCII{} Vertical Tab (VT)}
345\lineii{\e\var{ooo}} {\ASCII{} character with octal value \emph{ooo}}
346\lineii{\e x\var{hh...}} {\ASCII{} character with hex value \emph{hh...}}
347\end{tableii}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000348\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000349
Fred Drake5c07d9b1998-05-14 19:37:06 +0000350In strict compatibility with Standard \C, up to three octal digits are
Fred Drakef6669171998-05-06 19:52:49 +0000351accepted, but an unlimited number of hex digits is taken to be part of
352the hex escape (and then the lower 8 bits of the resulting hex number
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000353are used in 8-bit implementations).
Fred Drakef6669171998-05-06 19:52:49 +0000354
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000355Unlike Standard \C{},
356all unrecognized escape sequences are left in the string unchanged,
Fred Drake5c07d9b1998-05-14 19:37:06 +0000357i.e., \emph{the backslash is left in the string.} (This behavior is
Fred Drakef6669171998-05-06 19:52:49 +0000358useful when debugging: if an escape sequence is mistyped, the
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000359resulting output is more easily recognized as broken.)
Fred Drakef6669171998-05-06 19:52:49 +0000360\index{unrecognized escape sequence}
361
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000362When an `r' or `R' prefix is present, backslashes are still used to
363quote the following character, but \emph{all backslashes are left in
364the string}. For example, the string literal \code{r"\e n"} consists
365of two characters: a backslash and a lowercase `n'. String quotes can
366be escaped with a backslash, but the backslash remains in the string;
Fred Drakec456d361998-10-01 20:41:57 +0000367for example, \code{r"\e""} is a valid string literal consisting of two
368characters: a backslash and a double quote; \code{r"\e"} is not a value
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000369string literal (even a raw string cannot end in an odd number of
370backslashes). Specifically, \emph{a raw string cannot end in a single
371backslash} (since the backslash would escape the following quote
Fred Drakee15956b2000-04-03 04:51:13 +0000372character). Note also that a single backslash followed by a newline
373is interpreted as those two characters as part of the string,
374\emph{not} as a line continuation.
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000375
Fred Drake61c77281998-07-28 19:34:22 +0000376\subsection{String literal concatenation\label{string-catenation}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000377
378Multiple adjacent string literals (delimited by whitespace), possibly
379using different quoting conventions, are allowed, and their meaning is
380the same as their concatenation. Thus, \code{"hello" 'world'} is
381equivalent to \code{"helloworld"}. This feature can be used to reduce
382the number of backslashes needed, to split long strings conveniently
383across long lines, or even to add comments to parts of strings, for
384example:
385
386\begin{verbatim}
387re.compile("[A-Za-z_]" # letter or underscore
388 "[A-Za-z0-9_]*" # letter, digit or underscore
389 )
390\end{verbatim}
391
392Note that this feature is defined at the syntactical level, but
393implemented at compile time. The `+' operator must be used to
394concatenate string expressions at run time. Also note that literal
395concatenation can use different quoting styles for each component
396(even mixing raw strings and triple quoted strings).
397
Fred Drake61c77281998-07-28 19:34:22 +0000398\subsection{Numeric literals\label{numbers}}
Fred Drakef6669171998-05-06 19:52:49 +0000399
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000400There are four types of numeric literals: plain integers, long
401integers, floating point numbers, and imaginary numbers. There are no
402complex literals (complex numbers can be formed by adding a real
403number and an imaginary number).
Fred Drakef6669171998-05-06 19:52:49 +0000404\index{number}
405\index{numeric literal}
406\index{integer literal}
407\index{plain integer literal}
408\index{long integer literal}
409\index{floating point literal}
410\index{hexadecimal literal}
411\index{octal literal}
412\index{decimal literal}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000413\index{imaginary literal}
414\index{complex literal}
415
416Note that numeric literals do not include a sign; a phrase like
417\code{-1} is actually an expression composed of the unary operator
418`\code{-}' and the literal \code{1}.
419
Fred Drake61c77281998-07-28 19:34:22 +0000420\subsection{Integer and long integer literals\label{integers}}
Fred Drakef6669171998-05-06 19:52:49 +0000421
422Integer and long integer literals are described by the following
423lexical definitions:
424
425\begin{verbatim}
426longinteger: integer ("l"|"L")
427integer: decimalinteger | octinteger | hexinteger
428decimalinteger: nonzerodigit digit* | "0"
429octinteger: "0" octdigit+
430hexinteger: "0" ("x"|"X") hexdigit+
Fred Drakef6669171998-05-06 19:52:49 +0000431nonzerodigit: "1"..."9"
432octdigit: "0"..."7"
433hexdigit: digit|"a"..."f"|"A"..."F"
434\end{verbatim}
435
436Although both lower case `l' and upper case `L' are allowed as suffix
437for long integers, it is strongly recommended to always use `L', since
438the letter `l' looks too much like the digit `1'.
439
440Plain integer decimal literals must be at most 2147483647 (i.e., the
441largest positive integer, using 32-bit arithmetic). Plain octal and
442hexadecimal literals may be as large as 4294967295, but values larger
443than 2147483647 are converted to a negative value by subtracting
4444294967296. There is no limit for long integer literals apart from
445what can be stored in available memory.
446
447Some examples of plain and long integer literals:
448
449\begin{verbatim}
4507 2147483647 0177 0x80000000
4513L 79228162514264337593543950336L 0377L 0x100000000L
452\end{verbatim}
453
Fred Drake61c77281998-07-28 19:34:22 +0000454\subsection{Floating point literals\label{floating}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000455
Fred Drakef6669171998-05-06 19:52:49 +0000456Floating point literals are described by the following lexical
457definitions:
458
459\begin{verbatim}
460floatnumber: pointfloat | exponentfloat
461pointfloat: [intpart] fraction | intpart "."
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000462exponentfloat: (nonzerodigit digit* | pointfloat) exponent
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000463intpart: nonzerodigit digit* | "0"
Fred Drakef6669171998-05-06 19:52:49 +0000464fraction: "." digit+
465exponent: ("e"|"E") ["+"|"-"] digit+
466\end{verbatim}
467
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000468Note that the integer part of a floating point number cannot look like
Fred Drakee15956b2000-04-03 04:51:13 +0000469an octal integer, though the exponent may look like an octal literal
470but will always be interpreted using radix 10. For example,
471\samp{1e010} is legal, while \samp{07.1} is a syntax error.
Fred Drakef6669171998-05-06 19:52:49 +0000472The allowed range of floating point literals is
473implementation-dependent.
Fred Drakef6669171998-05-06 19:52:49 +0000474Some examples of floating point literals:
475
476\begin{verbatim}
4773.14 10. .001 1e100 3.14e-10
478\end{verbatim}
479
480Note that numeric literals do not include a sign; a phrase like
Fred Drake5c07d9b1998-05-14 19:37:06 +0000481\code{-1} is actually an expression composed of the operator
482\code{-} and the literal \code{1}.
Fred Drakef6669171998-05-06 19:52:49 +0000483
Fred Drake61c77281998-07-28 19:34:22 +0000484\subsection{Imaginary literals\label{imaginary}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000485
486Imaginary literals are described by the following lexical definitions:
487
488\begin{verbatim}
489imagnumber: (floatnumber | intpart) ("j"|"J")
490\end{verbatim}
491
Fred Drakee15956b2000-04-03 04:51:13 +0000492An imaginary literal yields a complex number with a real part of
Guido van Rossum60f2f0c1998-06-15 18:00:50 +00004930.0. Complex numbers are represented as a pair of floating point
494numbers and have the same restrictions on their range. To create a
495complex number with a nonzero real part, add a floating point number
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000496to it, e.g., \code{(3+4j)}. Some examples of imaginary literals:
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000497
498\begin{verbatim}
Guido van Rossum7c0240f1998-07-24 15:36:43 +00004993.14j 10.j 10j .001j 1e100j 3.14e-10j
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000500\end{verbatim}
501
502
Fred Drake61c77281998-07-28 19:34:22 +0000503\section{Operators\label{operators}}
Fred Drakef6669171998-05-06 19:52:49 +0000504
505The following tokens are operators:
506\index{operators}
507
508\begin{verbatim}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000509+ - * ** / %
Fred Drakef6669171998-05-06 19:52:49 +0000510<< >> & | ^ ~
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000511< > <= >= == != <>
Fred Drakef6669171998-05-06 19:52:49 +0000512\end{verbatim}
513
Fred Drake5c07d9b1998-05-14 19:37:06 +0000514The comparison operators \code{<>} and \code{!=} are alternate
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000515spellings of the same operator. \code{!=} is the preferred spelling;
516\code{<>} is obsolescent.
Fred Drakef6669171998-05-06 19:52:49 +0000517
Fred Drake61c77281998-07-28 19:34:22 +0000518\section{Delimiters\label{delimiters}}
Fred Drakef6669171998-05-06 19:52:49 +0000519
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000520The following tokens serve as delimiters in the grammar:
Fred Drakef6669171998-05-06 19:52:49 +0000521\index{delimiters}
522
523\begin{verbatim}
524( ) [ ] { }
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000525, : . ` = ;
Thomas Wouters12bba852000-08-24 20:06:04 +0000526+= -= *= /= %= **=
527&= |= ^= >>= <<=
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000528\end{verbatim}
529
530The period can also occur in floating-point and imaginary literals. A
Fred Drakee15956b2000-04-03 04:51:13 +0000531sequence of three periods has a special meaning as an ellipsis in slices.
Thomas Wouters12bba852000-08-24 20:06:04 +0000532The second half of the list, the augmented assignment operators, serve
533lexically as delimiters, but also perform an operation.
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000534
535The following printing ASCII characters have special meaning as part
536of other tokens or are otherwise significant to the lexical analyzer:
537
538\begin{verbatim}
539' " # \
Fred Drakef6669171998-05-06 19:52:49 +0000540\end{verbatim}
541
542The following printing \ASCII{} characters are not used in Python. Their
543occurrence outside string literals and comments is an unconditional
544error:
Fred Drake5c07d9b1998-05-14 19:37:06 +0000545\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000546
547\begin{verbatim}
548@ $ ?
549\end{verbatim}