blob: e187c7aede9e03f8553386d110da2b018fe78953 [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 Drake61c77281998-07-28 19:34:22 +000092\subsection{Implicit line joining\label{implicit-joining}}
Fred Drakef6669171998-05-06 19:52:49 +000093
94Expressions in parentheses, square brackets or curly braces can be
95split over more than one physical line without using backslashes.
96For example:
97
98\begin{verbatim}
99month_names = ['Januari', 'Februari', 'Maart', # These are the
100 'April', 'Mei', 'Juni', # Dutch names
101 'Juli', 'Augustus', 'September', # for the months
102 'Oktober', 'November', 'December'] # of the year
103\end{verbatim}
104
105Implicitly continued lines can carry comments. The indentation of the
106continuation lines is not important. Blank continuation lines are
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000107allowed. There is no NEWLINE token between implicit continuation
108lines. Implicitly continued lines can also occur within triple-quoted
109strings (see below); in that case they cannot carry comments.
Fred Drakef6669171998-05-06 19:52:49 +0000110
Fred Drake61c77281998-07-28 19:34:22 +0000111\subsection{Blank lines\label{blank-lines}}
Fred Drakef6669171998-05-06 19:52:49 +0000112
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000113A logical line that contains only spaces, tabs, formfeeds and possibly a
Fred Drakef6669171998-05-06 19:52:49 +0000114comment, is ignored (i.e., no NEWLINE token is generated), except that
115during interactive input of statements, an entirely blank logical line
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000116(i.e. one containing not even whitespace or a comment)
Fred Drakef6669171998-05-06 19:52:49 +0000117terminates a multi-line statement.
118\index{blank line}
119
Fred Drake61c77281998-07-28 19:34:22 +0000120\subsection{Indentation\label{indentation}}
Fred Drakef6669171998-05-06 19:52:49 +0000121
122Leading whitespace (spaces and tabs) at the beginning of a logical
123line is used to compute the indentation level of the line, which in
124turn is used to determine the grouping of statements.
125\index{indentation}
126\index{whitespace}
127\index{leading whitespace}
128\index{space}
129\index{tab}
130\index{grouping}
131\index{statement grouping}
132
133First, tabs are replaced (from left to right) by one to eight spaces
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000134such that the total number of characters up to and including the
135replacement is a multiple of
Fred Drake5c07d9b1998-05-14 19:37:06 +0000136eight (this is intended to be the same rule as used by \UNIX{}). The
Fred Drakef6669171998-05-06 19:52:49 +0000137total number of spaces preceding the first non-blank character then
138determines the line's indentation. Indentation cannot be split over
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000139multiple physical lines using backslashes; the whitespace up to the
140first backslash determines the indentation.
141
142\strong{Cross-platform compatibility note:} because of the nature of
143text editors on non-UNIX platforms, it is unwise to use a mixture of
144spaces and tabs for the indentation in a single source file.
145
146A formfeed character may be present at the start of the line; it will
147be ignored for the indentation calculations above. A formfeed
148characters occurring elsewhere in the leading whitespace have an
149undefined effect (for instance, they may reset the space count to
150zero).
Fred Drakef6669171998-05-06 19:52:49 +0000151
152The indentation levels of consecutive lines are used to generate
153INDENT and DEDENT tokens, using a stack, as follows.
154\index{INDENT token}
155\index{DEDENT token}
156
157Before the first line of the file is read, a single zero is pushed on
158the stack; this will never be popped off again. The numbers pushed on
159the stack will always be strictly increasing from bottom to top. At
160the beginning of each logical line, the line's indentation level is
161compared to the top of the stack. If it is equal, nothing happens.
162If it is larger, it is pushed on the stack, and one INDENT token is
Fred Drake5c07d9b1998-05-14 19:37:06 +0000163generated. If it is smaller, it \emph{must} be one of the numbers
Fred Drakef6669171998-05-06 19:52:49 +0000164occurring on the stack; all numbers on the stack that are larger are
165popped off, and for each number popped off a DEDENT token is
166generated. At the end of the file, a DEDENT token is generated for
167each number remaining on the stack that is larger than zero.
168
169Here is an example of a correctly (though confusingly) indented piece
170of Python code:
171
172\begin{verbatim}
173def perm(l):
174 # Compute the list of all permutations of l
Fred Drakef6669171998-05-06 19:52:49 +0000175 if len(l) <= 1:
176 return [l]
177 r = []
178 for i in range(len(l)):
179 s = l[:i] + l[i+1:]
180 p = perm(s)
181 for x in p:
182 r.append(l[i:i+1] + x)
183 return r
184\end{verbatim}
185
186The following example shows various indentation errors:
187
188\begin{verbatim}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000189 def perm(l): # error: first line indented
Fred Drakef6669171998-05-06 19:52:49 +0000190 for i in range(len(l)): # error: not indented
191 s = l[:i] + l[i+1:]
192 p = perm(l[:i] + l[i+1:]) # error: unexpected indent
193 for x in p:
194 r.append(l[i:i+1] + x)
195 return r # error: inconsistent dedent
196\end{verbatim}
197
198(Actually, the first three errors are detected by the parser; only the
199last error is found by the lexical analyzer --- the indentation of
Fred Drake5c07d9b1998-05-14 19:37:06 +0000200\code{return r} does not match a level popped off the stack.)
Fred Drakef6669171998-05-06 19:52:49 +0000201
Fred Drake61c77281998-07-28 19:34:22 +0000202\subsection{Whitespace between tokens\label{whitespace}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000203
204Except at the beginning of a logical line or in string literals, the
205whitespace characters space, tab and formfeed can be used
206interchangeably to separate tokens. Whitespace is needed between two
207tokens only if their concatenation could otherwise be interpreted as a
208different token (e.g., ab is one token, but a b is two tokens).
209
Fred Drake61c77281998-07-28 19:34:22 +0000210\section{Other tokens\label{other-tokens}}
Fred Drakef6669171998-05-06 19:52:49 +0000211
212Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000213exist: \emph{identifiers}, \emph{keywords}, \emph{literals},
214\emph{operators}, and \emph{delimiters}.
215Whitespace characters (other than line terminators, discussed earlier)
216are not tokens, but serve to delimit tokens.
217Where
Fred Drakef6669171998-05-06 19:52:49 +0000218ambiguity exists, a token comprises the longest possible string that
219forms a legal token, when read from left to right.
220
Fred Drake61c77281998-07-28 19:34:22 +0000221\section{Identifiers and keywords\label{identifiers}}
Fred Drakef6669171998-05-06 19:52:49 +0000222
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000223Identifiers (also referred to as \emph{names}) are described by the following
Fred Drakef6669171998-05-06 19:52:49 +0000224lexical definitions:
225\index{identifier}
226\index{name}
227
228\begin{verbatim}
229identifier: (letter|"_") (letter|digit|"_")*
230letter: lowercase | uppercase
231lowercase: "a"..."z"
232uppercase: "A"..."Z"
233digit: "0"..."9"
234\end{verbatim}
235
236Identifiers are unlimited in length. Case is significant.
237
Fred Drake61c77281998-07-28 19:34:22 +0000238\subsection{Keywords\label{keywords}}
Fred Drakef6669171998-05-06 19:52:49 +0000239
Fred Drake5c07d9b1998-05-14 19:37:06 +0000240The following identifiers are used as reserved words, or
241\emph{keywords} of the language, and cannot be used as ordinary
242identifiers. They must be spelled exactly as written here:%
243\index{keyword}%
Fred Drakef6669171998-05-06 19:52:49 +0000244\index{reserved word}
245
246\begin{verbatim}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000247and del for is raise
248assert elif from lambda return
249break else global not try
250class except if or while
251continue exec import pass
252def finally in print
Fred Drakef6669171998-05-06 19:52:49 +0000253\end{verbatim}
254
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000255% When adding keywords, use reswords.py for reformatting
256
Fred Drake61c77281998-07-28 19:34:22 +0000257\subsection{Reserved classes of identifiers\label{id-classes}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000258
259Certain classes of identifiers (besides keywords) have special
260meanings. These are:
261
Fred Drakea1cce711998-07-24 22:12:32 +0000262\begin{tableii}{l|l}{code}{Form}{Meaning}
263\lineii{_*}{Not imported by \samp{from \var{module} import *}}
264\lineii{__*__}{System-defined name}
265\lineii{__*}{Class-private name mangling}
266\end{tableii}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000267
268(XXX need section references here.)
Fred Drakef6669171998-05-06 19:52:49 +0000269
Fred Drake61c77281998-07-28 19:34:22 +0000270\section{Literals\label{literals}}
Fred Drakef6669171998-05-06 19:52:49 +0000271
272Literals are notations for constant values of some built-in types.
273\index{literal}
274\index{constant}
275
Fred Drake61c77281998-07-28 19:34:22 +0000276\subsection{String literals\label{strings}}
Fred Drakef6669171998-05-06 19:52:49 +0000277
278String literals are described by the following lexical definitions:
279\index{string literal}
280
281\begin{verbatim}
282stringliteral: shortstring | longstring
283shortstring: "'" shortstringitem* "'" | '"' shortstringitem* '"'
284longstring: "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
285shortstringitem: shortstringchar | escapeseq
286longstringitem: longstringchar | escapeseq
287shortstringchar: <any ASCII character except "\" or newline or the quote>
288longstringchar: <any ASCII character except "\">
289escapeseq: "\" <any ASCII character>
290\end{verbatim}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000291\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000292
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000293In plain English: String literals can be enclosed in matching single
294quotes (\code{'}) or double quotes (\code{"}). They can also be
295enclosed in matching groups of three single or double quotes (these
296are generally referred to as \emph{triple-quoted strings}). The
297backslash (\code{\e}) character is used to escape characters that
298otherwise have a special meaning, such as newline, backslash itself,
299or the quote character. String literals may optionally be prefixed
300with a letter `r' or `R'; such strings are called raw strings and use
301different rules for backslash escape sequences.
302\index{triple-quoted string}
303\index{raw string}
304
305In triple-quoted strings,
Fred Drakef6669171998-05-06 19:52:49 +0000306unescaped newlines and quotes are allowed (and are retained), except
307that three unescaped quotes in a row terminate the string. (A
308``quote'' is the character used to open the string, i.e. either
Fred Drake5c07d9b1998-05-14 19:37:06 +0000309\code{'} or \code{"}.)
Fred Drakef6669171998-05-06 19:52:49 +0000310
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000311Unless an `r' or `R' prefix is present, escape sequences in strings
312are interpreted according to rules similar
313to those used by Standard \C{}. The recognized escape sequences are:
Fred Drakef6669171998-05-06 19:52:49 +0000314\index{physical line}
315\index{escape sequence}
316\index{Standard C}
317\index{C}
318
Fred Drakea1cce711998-07-24 22:12:32 +0000319\begin{tableii}{l|l}{code}{Escape Sequence}{Meaning}
320\lineii{\e\var{newline}} {Ignored}
321\lineii{\e\e} {Backslash (\code{\e})}
322\lineii{\e'} {Single quote (\code{'})}
323\lineii{\e"} {Double quote (\code{"})}
324\lineii{\e a} {\ASCII{} Bell (BEL)}
325\lineii{\e b} {\ASCII{} Backspace (BS)}
326\lineii{\e f} {\ASCII{} Formfeed (FF)}
327\lineii{\e n} {\ASCII{} Linefeed (LF)}
328\lineii{\e r} {\ASCII{} Carriage Return (CR)}
329\lineii{\e t} {\ASCII{} Horizontal Tab (TAB)}
330\lineii{\e v} {\ASCII{} Vertical Tab (VT)}
331\lineii{\e\var{ooo}} {\ASCII{} character with octal value \emph{ooo}}
332\lineii{\e x\var{hh...}} {\ASCII{} character with hex value \emph{hh...}}
333\end{tableii}
Fred Drake5c07d9b1998-05-14 19:37:06 +0000334\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000335
Fred Drake5c07d9b1998-05-14 19:37:06 +0000336In strict compatibility with Standard \C, up to three octal digits are
Fred Drakef6669171998-05-06 19:52:49 +0000337accepted, but an unlimited number of hex digits is taken to be part of
338the hex escape (and then the lower 8 bits of the resulting hex number
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000339are used in 8-bit implementations).
Fred Drakef6669171998-05-06 19:52:49 +0000340
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000341Unlike Standard \C{},
342all unrecognized escape sequences are left in the string unchanged,
Fred Drake5c07d9b1998-05-14 19:37:06 +0000343i.e., \emph{the backslash is left in the string.} (This behavior is
Fred Drakef6669171998-05-06 19:52:49 +0000344useful when debugging: if an escape sequence is mistyped, the
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000345resulting output is more easily recognized as broken.)
Fred Drakef6669171998-05-06 19:52:49 +0000346\index{unrecognized escape sequence}
347
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000348When an `r' or `R' prefix is present, backslashes are still used to
349quote the following character, but \emph{all backslashes are left in
350the string}. For example, the string literal \code{r"\e n"} consists
351of two characters: a backslash and a lowercase `n'. String quotes can
352be escaped with a backslash, but the backslash remains in the string;
353for example, \code{r"\""} is a valid string literal consisting of two
354characters: a backslash and a double quote; \code{r"\"} is not a value
355string literal (even a raw string cannot end in an odd number of
356backslashes). Specifically, \emph{a raw string cannot end in a single
357backslash} (since the backslash would escape the following quote
358character).
359
Fred Drake61c77281998-07-28 19:34:22 +0000360\subsection{String literal concatenation\label{string-catenation}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000361
362Multiple adjacent string literals (delimited by whitespace), possibly
363using different quoting conventions, are allowed, and their meaning is
364the same as their concatenation. Thus, \code{"hello" 'world'} is
365equivalent to \code{"helloworld"}. This feature can be used to reduce
366the number of backslashes needed, to split long strings conveniently
367across long lines, or even to add comments to parts of strings, for
368example:
369
370\begin{verbatim}
371re.compile("[A-Za-z_]" # letter or underscore
372 "[A-Za-z0-9_]*" # letter, digit or underscore
373 )
374\end{verbatim}
375
376Note that this feature is defined at the syntactical level, but
377implemented at compile time. The `+' operator must be used to
378concatenate string expressions at run time. Also note that literal
379concatenation can use different quoting styles for each component
380(even mixing raw strings and triple quoted strings).
381
Fred Drake61c77281998-07-28 19:34:22 +0000382\subsection{Numeric literals\label{numbers}}
Fred Drakef6669171998-05-06 19:52:49 +0000383
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000384There are four types of numeric literals: plain integers, long
385integers, floating point numbers, and imaginary numbers. There are no
386complex literals (complex numbers can be formed by adding a real
387number and an imaginary number).
Fred Drakef6669171998-05-06 19:52:49 +0000388\index{number}
389\index{numeric literal}
390\index{integer literal}
391\index{plain integer literal}
392\index{long integer literal}
393\index{floating point literal}
394\index{hexadecimal literal}
395\index{octal literal}
396\index{decimal literal}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000397\index{imaginary literal}
398\index{complex literal}
399
400Note that numeric literals do not include a sign; a phrase like
401\code{-1} is actually an expression composed of the unary operator
402`\code{-}' and the literal \code{1}.
403
Fred Drake61c77281998-07-28 19:34:22 +0000404\subsection{Integer and long integer literals\label{integers}}
Fred Drakef6669171998-05-06 19:52:49 +0000405
406Integer and long integer literals are described by the following
407lexical definitions:
408
409\begin{verbatim}
410longinteger: integer ("l"|"L")
411integer: decimalinteger | octinteger | hexinteger
412decimalinteger: nonzerodigit digit* | "0"
413octinteger: "0" octdigit+
414hexinteger: "0" ("x"|"X") hexdigit+
Fred Drakef6669171998-05-06 19:52:49 +0000415nonzerodigit: "1"..."9"
416octdigit: "0"..."7"
417hexdigit: digit|"a"..."f"|"A"..."F"
418\end{verbatim}
419
420Although both lower case `l' and upper case `L' are allowed as suffix
421for long integers, it is strongly recommended to always use `L', since
422the letter `l' looks too much like the digit `1'.
423
424Plain integer decimal literals must be at most 2147483647 (i.e., the
425largest positive integer, using 32-bit arithmetic). Plain octal and
426hexadecimal literals may be as large as 4294967295, but values larger
427than 2147483647 are converted to a negative value by subtracting
4284294967296. There is no limit for long integer literals apart from
429what can be stored in available memory.
430
431Some examples of plain and long integer literals:
432
433\begin{verbatim}
4347 2147483647 0177 0x80000000
4353L 79228162514264337593543950336L 0377L 0x100000000L
436\end{verbatim}
437
Fred Drake61c77281998-07-28 19:34:22 +0000438\subsection{Floating point literals\label{floating}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000439
Fred Drakef6669171998-05-06 19:52:49 +0000440Floating point literals are described by the following lexical
441definitions:
442
443\begin{verbatim}
444floatnumber: pointfloat | exponentfloat
445pointfloat: [intpart] fraction | intpart "."
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000446exponentfloat: (nonzerodigit digit* | pointfloat) exponent
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000447intpart: nonzerodigit digit* | "0"
Fred Drakef6669171998-05-06 19:52:49 +0000448fraction: "." digit+
449exponent: ("e"|"E") ["+"|"-"] digit+
450\end{verbatim}
451
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000452Note that the integer part of a floating point number cannot look like
453an octal integer.
Fred Drakef6669171998-05-06 19:52:49 +0000454The allowed range of floating point literals is
455implementation-dependent.
Fred Drakef6669171998-05-06 19:52:49 +0000456Some examples of floating point literals:
457
458\begin{verbatim}
4593.14 10. .001 1e100 3.14e-10
460\end{verbatim}
461
462Note that numeric literals do not include a sign; a phrase like
Fred Drake5c07d9b1998-05-14 19:37:06 +0000463\code{-1} is actually an expression composed of the operator
464\code{-} and the literal \code{1}.
Fred Drakef6669171998-05-06 19:52:49 +0000465
Fred Drake61c77281998-07-28 19:34:22 +0000466\subsection{Imaginary literals\label{imaginary}}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000467
468Imaginary literals are described by the following lexical definitions:
469
470\begin{verbatim}
471imagnumber: (floatnumber | intpart) ("j"|"J")
472\end{verbatim}
473
474An imaginary literals yields a complex number with a real part of
4750.0. Complex numbers are represented as a pair of floating point
476numbers and have the same restrictions on their range. To create a
477complex number with a nonzero real part, add a floating point number
Guido van Rossum7c0240f1998-07-24 15:36:43 +0000478to it, e.g., \code{(3+4j)}. Some examples of imaginary literals:
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000479
480\begin{verbatim}
Guido van Rossum7c0240f1998-07-24 15:36:43 +00004813.14j 10.j 10j .001j 1e100j 3.14e-10j
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000482\end{verbatim}
483
484
Fred Drake61c77281998-07-28 19:34:22 +0000485\section{Operators\label{operators}}
Fred Drakef6669171998-05-06 19:52:49 +0000486
487The following tokens are operators:
488\index{operators}
489
490\begin{verbatim}
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000491+ - * ** / %
Fred Drakef6669171998-05-06 19:52:49 +0000492<< >> & | ^ ~
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000493< > <= >= == != <>
Fred Drakef6669171998-05-06 19:52:49 +0000494\end{verbatim}
495
Fred Drake5c07d9b1998-05-14 19:37:06 +0000496The comparison operators \code{<>} and \code{!=} are alternate
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000497spellings of the same operator. \code{!=} is the preferred spelling;
498\code{<>} is obsolescent.
Fred Drakef6669171998-05-06 19:52:49 +0000499
Fred Drake61c77281998-07-28 19:34:22 +0000500\section{Delimiters\label{delimiters}}
Fred Drakef6669171998-05-06 19:52:49 +0000501
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000502The following tokens serve as delimiters in the grammar:
Fred Drakef6669171998-05-06 19:52:49 +0000503\index{delimiters}
504
505\begin{verbatim}
506( ) [ ] { }
Guido van Rossum60f2f0c1998-06-15 18:00:50 +0000507, : . ` = ;
508\end{verbatim}
509
510The period can also occur in floating-point and imaginary literals. A
511sequence of three periods has a special meaning as ellipses in slices.
512
513The following printing ASCII characters have special meaning as part
514of other tokens or are otherwise significant to the lexical analyzer:
515
516\begin{verbatim}
517' " # \
Fred Drakef6669171998-05-06 19:52:49 +0000518\end{verbatim}
519
520The following printing \ASCII{} characters are not used in Python. Their
521occurrence outside string literals and comments is an unconditional
522error:
Fred Drake5c07d9b1998-05-14 19:37:06 +0000523\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +0000524
525\begin{verbatim}
526@ $ ?
527\end{verbatim}