Mercurial > cpython-lru_cache-weakref
annotate Doc/ref/ref2.tex @ 21467:4e35fa5d5708 legacy-trunk
Add documentation for the help() built-in; contributed by Ka-Ping Yee.
This is part of SF patch #494622.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Tue, 18 Dec 2001 16:31:08 +0000 |
| parents | d1f938eee700 |
| children | cc788b3b21b6 |
| rev | line source |
|---|---|
|
8752
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
1 \chapter{Lexical analysis\label{lexical}} |
| 8231 | 2 |
|
8337
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
3 A Python program is read by a \emph{parser}. Input to the parser is a |
|
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
4 stream of \emph{tokens}, generated by the \emph{lexical analyzer}. This |
| 8231 | 5 chapter describes how the lexical analyzer breaks a file into tokens. |
| 6 \index{lexical analysis} | |
| 7 \index{parser} | |
| 8 \index{token} | |
| 9 | |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
10 Python uses the 7-bit \ASCII{} character set for program text and string |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
11 literals. 8-bit characters may be used in string literals and comments |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
12 but their interpretation is platform dependent; the proper way to |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
13 insert 8-bit characters in string literals is by using octal or |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
14 hexadecimal escape sequences. |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
15 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
16 The run-time character set depends on the I/O devices connected to the |
|
21062
bc02c3b09626
Clean up some markup cruft. A number of the macros that take no
Fred Drake <fdrake@acm.org>
parents:
19621
diff
changeset
|
17 program but is generally a superset of \ASCII. |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
18 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
19 \strong{Future compatibility note:} It may be tempting to assume that the |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
20 character set for 8-bit characters is ISO Latin-1 (an \ASCII{} |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
21 superset that covers most western languages that use the Latin |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
22 alphabet), but it is possible that in the future Unicode text editors |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
23 will become common. These generally use the UTF-8 encoding, which is |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
24 also an \ASCII{} superset, but with very different use for the |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
25 characters with ordinals 128-255. While there is no consensus on this |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
26 subject yet, it is unwise to assume either Latin-1 or UTF-8, even |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
27 though the current implementation appears to favor Latin-1. This |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
28 applies both to the source character set and the run-time character |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
29 set. |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
30 |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
31 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
32 \section{Line structure\label{line-structure}} |
| 8231 | 33 |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
34 A Python program is divided into a number of \emph{logical lines}. |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
35 \index{line structure} |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
36 |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
37 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
38 \subsection{Logical lines\label{logical}} |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
39 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
40 The end of |
| 8231 | 41 a logical line is represented by the token NEWLINE. Statements cannot |
| 42 cross logical line boundaries except where NEWLINE is allowed by the | |
| 8736 | 43 syntax (e.g., between statements in compound statements). |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
44 A logical line is constructed from one or more \emph{physical lines} |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
45 by following the explicit or implicit \emph{line joining} rules. |
| 8231 | 46 \index{logical line} |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
47 \index{physical line} |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
48 \index{line joining} |
| 8231 | 49 \index{NEWLINE token} |
| 50 | |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
51 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
52 \subsection{Physical lines\label{physical}} |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
53 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
54 A physical line ends in whatever the current platform's convention is |
|
21062
bc02c3b09626
Clean up some markup cruft. A number of the macros that take no
Fred Drake <fdrake@acm.org>
parents:
19621
diff
changeset
|
55 for terminating lines. On \UNIX, this is the \ASCII{} LF (linefeed) |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
56 character. On DOS/Windows, it is the \ASCII{} sequence CR LF (return |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
57 followed by linefeed). On Macintosh, it is the \ASCII{} CR (return) |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
58 character. |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
59 |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
60 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
61 \subsection{Comments\label{comments}} |
| 8231 | 62 |
|
8337
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
63 A comment starts with a hash character (\code{\#}) that is not part of |
| 8231 | 64 a string literal, and ends at the end of the physical line. A comment |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
65 signifies the end of the logical line unless the implicit line joining |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
66 rules are invoked. |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
67 Comments are ignored by the syntax; they are not tokens. |
| 8231 | 68 \index{comment} |
| 69 \index{hash character} | |
| 70 | |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
71 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
72 \subsection{Explicit line joining\label{explicit-joining}} |
| 8231 | 73 |
| 74 Two or more physical lines may be joined into logical lines using | |
|
8337
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
75 backslash characters (\code{\e}), as follows: when a physical line ends |
| 8231 | 76 in a backslash that is not part of a string literal or comment, it is |
| 77 joined with the following forming a single logical line, deleting the | |
| 78 backslash 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} | |
| 85 if 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 | |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
91 A line ending in a backslash cannot carry a comment. A backslash does |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
92 not continue a comment. A backslash does not continue a token except |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
93 for string literals (i.e., tokens other than string literals cannot be |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
94 split across physical lines using a backslash). A backslash is |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
95 illegal elsewhere on a line outside a string literal. |
| 8231 | 96 |
|
10258
163fd3d95c17
Revised description of blank line handling for the interpreter, based
Fred Drake <fdrake@acm.org>
parents:
9202
diff
changeset
|
97 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
98 \subsection{Implicit line joining\label{implicit-joining}} |
| 8231 | 99 |
| 100 Expressions in parentheses, square brackets or curly braces can be | |
| 101 split over more than one physical line without using backslashes. | |
| 102 For example: | |
| 103 | |
| 104 \begin{verbatim} | |
| 105 month_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 | |
| 111 Implicitly continued lines can carry comments. The indentation of the | |
| 112 continuation lines is not important. Blank continuation lines are | |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
113 allowed. There is no NEWLINE token between implicit continuation |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
114 lines. Implicitly continued lines can also occur within triple-quoted |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
115 strings (see below); in that case they cannot carry comments. |
| 8231 | 116 |
|
10258
163fd3d95c17
Revised description of blank line handling for the interpreter, based
Fred Drake <fdrake@acm.org>
parents:
9202
diff
changeset
|
117 |
|
163fd3d95c17
Revised description of blank line handling for the interpreter, based
Fred Drake <fdrake@acm.org>
parents:
9202
diff
changeset
|
118 \subsection{Blank lines \index{blank line}\label{blank-lines}} |
| 8231 | 119 |
|
10258
163fd3d95c17
Revised description of blank line handling for the interpreter, based
Fred Drake <fdrake@acm.org>
parents:
9202
diff
changeset
|
120 A logical line that contains only spaces, tabs, formfeeds and possibly |
|
163fd3d95c17
Revised description of blank line handling for the interpreter, based
Fred Drake <fdrake@acm.org>
parents:
9202
diff
changeset
|
121 a comment, is ignored (i.e., no NEWLINE token is generated). During |
|
163fd3d95c17
Revised description of blank line handling for the interpreter, based
Fred Drake <fdrake@acm.org>
parents:
9202
diff
changeset
|
122 interactive input of statements, handling of a blank line may differ |
|
163fd3d95c17
Revised description of blank line handling for the interpreter, based
Fred Drake <fdrake@acm.org>
parents:
9202
diff
changeset
|
123 depending on the implementation of the read-eval-print loop. In the |
|
163fd3d95c17
Revised description of blank line handling for the interpreter, based
Fred Drake <fdrake@acm.org>
parents:
9202
diff
changeset
|
124 standard implementation, an entirely blank logical line (i.e.\ one |
|
163fd3d95c17
Revised description of blank line handling for the interpreter, based
Fred Drake <fdrake@acm.org>
parents:
9202
diff
changeset
|
125 containing not even whitespace or a comment) terminates a multi-line |
|
163fd3d95c17
Revised description of blank line handling for the interpreter, based
Fred Drake <fdrake@acm.org>
parents:
9202
diff
changeset
|
126 statement. |
|
163fd3d95c17
Revised description of blank line handling for the interpreter, based
Fred Drake <fdrake@acm.org>
parents:
9202
diff
changeset
|
127 |
| 8231 | 128 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
129 \subsection{Indentation\label{indentation}} |
| 8231 | 130 |
| 131 Leading whitespace (spaces and tabs) at the beginning of a logical | |
| 132 line is used to compute the indentation level of the line, which in | |
| 133 turn 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 | |
| 142 First, tabs are replaced (from left to right) by one to eight spaces | |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
143 such that the total number of characters up to and including the |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
144 replacement is a multiple of |
|
21062
bc02c3b09626
Clean up some markup cruft. A number of the macros that take no
Fred Drake <fdrake@acm.org>
parents:
19621
diff
changeset
|
145 eight (this is intended to be the same rule as used by \UNIX). The |
| 8231 | 146 total number of spaces preceding the first non-blank character then |
| 147 determines the line's indentation. Indentation cannot be split over | |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
148 multiple physical lines using backslashes; the whitespace up to the |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
149 first backslash determines the indentation. |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
150 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
151 \strong{Cross-platform compatibility note:} because of the nature of |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
152 text editors on non-UNIX platforms, it is unwise to use a mixture of |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
153 spaces and tabs for the indentation in a single source file. |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
154 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
155 A formfeed character may be present at the start of the line; it will |
|
12217
5ea773413531
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
10356
diff
changeset
|
156 be ignored for the indentation calculations above. Formfeed |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
157 characters occurring elsewhere in the leading whitespace have an |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
158 undefined effect (for instance, they may reset the space count to |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
159 zero). |
| 8231 | 160 |
| 161 The indentation levels of consecutive lines are used to generate | |
| 162 INDENT and DEDENT tokens, using a stack, as follows. | |
| 163 \index{INDENT token} | |
| 164 \index{DEDENT token} | |
| 165 | |
| 166 Before the first line of the file is read, a single zero is pushed on | |
| 167 the stack; this will never be popped off again. The numbers pushed on | |
| 168 the stack will always be strictly increasing from bottom to top. At | |
| 169 the beginning of each logical line, the line's indentation level is | |
| 170 compared to the top of the stack. If it is equal, nothing happens. | |
| 171 If it is larger, it is pushed on the stack, and one INDENT token is | |
|
8337
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
172 generated. If it is smaller, it \emph{must} be one of the numbers |
| 8231 | 173 occurring on the stack; all numbers on the stack that are larger are |
| 174 popped off, and for each number popped off a DEDENT token is | |
| 175 generated. At the end of the file, a DEDENT token is generated for | |
| 176 each number remaining on the stack that is larger than zero. | |
| 177 | |
| 178 Here is an example of a correctly (though confusingly) indented piece | |
| 179 of Python code: | |
| 180 | |
| 181 \begin{verbatim} | |
| 182 def perm(l): | |
| 183 # Compute the list of all permutations of l | |
| 184 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 | |
| 195 The following example shows various indentation errors: | |
| 196 | |
| 197 \begin{verbatim} | |
|
21345
d1f938eee700
Fix whitespace in example of bad indentation: a comment indicated that one
Fred Drake <fdrake@acm.org>
parents:
21198
diff
changeset
|
198 def perm(l): # error: first line indented |
|
d1f938eee700
Fix whitespace in example of bad indentation: a comment indicated that one
Fred Drake <fdrake@acm.org>
parents:
21198
diff
changeset
|
199 for i in range(len(l)): # error: not indented |
|
d1f938eee700
Fix whitespace in example of bad indentation: a comment indicated that one
Fred Drake <fdrake@acm.org>
parents:
21198
diff
changeset
|
200 s = l[:i] + l[i+1:] |
|
d1f938eee700
Fix whitespace in example of bad indentation: a comment indicated that one
Fred Drake <fdrake@acm.org>
parents:
21198
diff
changeset
|
201 p = perm(l[:i] + l[i+1:]) # error: unexpected indent |
|
d1f938eee700
Fix whitespace in example of bad indentation: a comment indicated that one
Fred Drake <fdrake@acm.org>
parents:
21198
diff
changeset
|
202 for x in p: |
|
d1f938eee700
Fix whitespace in example of bad indentation: a comment indicated that one
Fred Drake <fdrake@acm.org>
parents:
21198
diff
changeset
|
203 r.append(l[i:i+1] + x) |
|
d1f938eee700
Fix whitespace in example of bad indentation: a comment indicated that one
Fred Drake <fdrake@acm.org>
parents:
21198
diff
changeset
|
204 return r # error: inconsistent dedent |
| 8231 | 205 \end{verbatim} |
| 206 | |
| 207 (Actually, the first three errors are detected by the parser; only the | |
| 208 last error is found by the lexical analyzer --- the indentation of | |
|
8337
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
209 \code{return r} does not match a level popped off the stack.) |
| 8231 | 210 |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
211 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
212 \subsection{Whitespace between tokens\label{whitespace}} |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
213 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
214 Except at the beginning of a logical line or in string literals, the |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
215 whitespace characters space, tab and formfeed can be used |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
216 interchangeably to separate tokens. Whitespace is needed between two |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
217 tokens only if their concatenation could otherwise be interpreted as a |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
218 different token (e.g., ab is one token, but a b is two tokens). |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
219 |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
220 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
221 \section{Other tokens\label{other-tokens}} |
| 8231 | 222 |
| 223 Besides NEWLINE, INDENT and DEDENT, the following categories of tokens | |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
224 exist: \emph{identifiers}, \emph{keywords}, \emph{literals}, |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
225 \emph{operators}, and \emph{delimiters}. |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
226 Whitespace characters (other than line terminators, discussed earlier) |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
227 are not tokens, but serve to delimit tokens. |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
228 Where |
| 8231 | 229 ambiguity exists, a token comprises the longest possible string that |
| 230 forms a legal token, when read from left to right. | |
| 231 | |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
232 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
233 \section{Identifiers and keywords\label{identifiers}} |
| 8231 | 234 |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
235 Identifiers (also referred to as \emph{names}) are described by the following |
| 8231 | 236 lexical definitions: |
| 237 \index{identifier} | |
| 238 \index{name} | |
| 239 | |
|
18429
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
240 \begin{productionlist} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
241 \production{identifier} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
242 {(\token{letter}|"_") (\token{letter} | \token{digit} | "_")*} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
243 \production{letter} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
244 {\token{lowercase} | \token{uppercase}} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
245 \production{lowercase} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
246 {"a"..."z"} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
247 \production{uppercase} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
248 {"A"..."Z"} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
249 \production{digit} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
250 {"0"..."9"} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
251 \end{productionlist} |
| 8231 | 252 |
| 253 Identifiers are unlimited in length. Case is significant. | |
| 254 | |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
255 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
256 \subsection{Keywords\label{keywords}} |
| 8231 | 257 |
|
8337
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
258 The following identifiers are used as reserved words, or |
|
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
259 \emph{keywords} of the language, and cannot be used as ordinary |
|
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
260 identifiers. They must be spelled exactly as written here:% |
|
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
261 \index{keyword}% |
| 8231 | 262 \index{reserved word} |
| 263 | |
| 264 \begin{verbatim} | |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
265 and del for is raise |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
266 assert elif from lambda return |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
267 break else global not try |
|
21198
f17a37de6285
Put the keywords back in alphabetical order. Apparently somebody
Guido van Rossum <guido@python.org>
parents:
21062
diff
changeset
|
268 class except if or while |
|
f17a37de6285
Put the keywords back in alphabetical order. Apparently somebody
Guido van Rossum <guido@python.org>
parents:
21062
diff
changeset
|
269 continue exec import pass yield |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
270 def finally in print |
| 8231 | 271 \end{verbatim} |
| 272 | |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
273 % When adding keywords, use reswords.py for reformatting |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
274 |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
275 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
276 \subsection{Reserved classes of identifiers\label{id-classes}} |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
277 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
278 Certain classes of identifiers (besides keywords) have special |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
279 meanings. These are: |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
280 |
|
10356
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
281 \begin{tableiii}{l|l|l}{code}{Form}{Meaning}{Notes} |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
282 \lineiii{_*}{Not imported by \samp{from \var{module} import *}}{(1)} |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
283 \lineiii{__*__}{System-defined name}{} |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
284 \lineiii{__*}{Class-private name mangling}{} |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
285 \end{tableiii} |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
286 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
287 (XXX need section references here.) |
| 8231 | 288 |
|
10356
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
289 Note: |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
290 |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
291 \begin{description} |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
292 \item[(1)] The special identifier \samp{_} is used in the interactive |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
293 interpreter to store the result of the last evaluation; it is stored |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
294 in the \module{__builtin__} module. When not in interactive mode, |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
295 \samp{_} has no special meaning and is not defined. |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
296 \end{description} |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
297 |
|
8923ad7e13f2
Added note about __builtin__._ to section dicussing classes of
Fred Drake <fdrake@acm.org>
parents:
10258
diff
changeset
|
298 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
299 \section{Literals\label{literals}} |
| 8231 | 300 |
| 301 Literals are notations for constant values of some built-in types. | |
| 302 \index{literal} | |
| 303 \index{constant} | |
| 304 | |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
305 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
306 \subsection{String literals\label{strings}} |
| 8231 | 307 |
| 308 String literals are described by the following lexical definitions: | |
| 309 \index{string literal} | |
| 310 | |
|
21062
bc02c3b09626
Clean up some markup cruft. A number of the macros that take no
Fred Drake <fdrake@acm.org>
parents:
19621
diff
changeset
|
311 \index{ASCII@\ASCII} |
|
18429
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
312 \begin{productionlist} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
313 \production{stringliteral} |
|
19257
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
314 {[\token{stringprefix}](\token{shortstring} | \token{longstring})} |
|
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
315 \production{stringprefix} |
|
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
316 {"r" | "u" | "ur" | "R" | "U" | "UR" | "Ur" | "uR"} |
|
18429
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
317 \production{shortstring} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
318 {"'" \token{shortstringitem}* "'" |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
319 | '"' \token{shortstringitem}* '"'} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
320 \production{longstring} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
321 {"'''" \token{longstringitem}* "'''" |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
322 | '"""' \token{longstringitem}* '"""'} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
323 \production{shortstringitem} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
324 {\token{shortstringchar} | \token{escapeseq}} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
325 \production{longstringitem} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
326 {\token{longstringchar} | \token{escapeseq}} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
327 \production{shortstringchar} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
328 {<any ASCII character except "\e" or newline or the quote>} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
329 \production{longstringchar} |
|
21345
d1f938eee700
Fix whitespace in example of bad indentation: a comment indicated that one
Fred Drake <fdrake@acm.org>
parents:
21198
diff
changeset
|
330 {<any ASCII character except "\e">} |
|
18429
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
331 \production{escapeseq} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
332 {"\e" <any ASCII character>} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
333 \end{productionlist} |
| 8231 | 334 |
|
19257
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
335 One syntactic restriction not indicated by these productions is that |
|
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
336 whitespace is not allowed between the \grammartoken{stringprefix} and |
|
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
337 the rest of the string literal. |
|
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
338 |
|
15619
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
339 \index{triple-quoted string} |
|
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
340 \index{Unicode Consortium} |
|
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
341 \index{string!Unicode} |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
342 In plain English: String literals can be enclosed in matching single |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
343 quotes (\code{'}) or double quotes (\code{"}). They can also be |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
344 enclosed in matching groups of three single or double quotes (these |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
345 are generally referred to as \emph{triple-quoted strings}). The |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
346 backslash (\code{\e}) character is used to escape characters that |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
347 otherwise have a special meaning, such as newline, backslash itself, |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
348 or the quote character. String literals may optionally be prefixed |
|
19257
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
349 with a letter `r' or `R'; such strings are called \dfn{raw |
|
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
350 strings}\index{raw string} and use different rules for interpreting |
|
15619
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
351 backslash escape sequences. A prefix of 'u' or 'U' makes the string |
|
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
352 a Unicode string. Unicode strings use the Unicode character set as |
|
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
353 defined by the Unicode Consortium and ISO~10646. Some additional |
|
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
354 escape sequences, described below, are available in Unicode strings. |
|
19257
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
355 The two prefix characters may be combined; in this case, `u' must |
|
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
356 appear before `r'. |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
357 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
358 In triple-quoted strings, |
| 8231 | 359 unescaped newlines and quotes are allowed (and are retained), except |
| 360 that three unescaped quotes in a row terminate the string. (A | |
| 361 ``quote'' is the character used to open the string, i.e. either | |
|
8337
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
362 \code{'} or \code{"}.) |
| 8231 | 363 |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
364 Unless an `r' or `R' prefix is present, escape sequences in strings |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
365 are interpreted according to rules similar |
|
18772
b10ff337896b
Removed unnecessary section "Unicode literals"; all the discussion is
Fred Drake <fdrake@acm.org>
parents:
18768
diff
changeset
|
366 to those used by Standard C. The recognized escape sequences are: |
| 8231 | 367 \index{physical line} |
| 368 \index{escape sequence} | |
| 369 \index{Standard C} | |
| 370 \index{C} | |
| 371 | |
|
8752
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
372 \begin{tableii}{l|l}{code}{Escape Sequence}{Meaning} |
|
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
373 \lineii{\e\var{newline}} {Ignored} |
|
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
374 \lineii{\e\e} {Backslash (\code{\e})} |
|
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
375 \lineii{\e'} {Single quote (\code{'})} |
|
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
376 \lineii{\e"} {Double quote (\code{"})} |
|
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
377 \lineii{\e a} {\ASCII{} Bell (BEL)} |
|
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
378 \lineii{\e b} {\ASCII{} Backspace (BS)} |
|
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
379 \lineii{\e f} {\ASCII{} Formfeed (FF)} |
|
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
380 \lineii{\e n} {\ASCII{} Linefeed (LF)} |
|
15619
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
381 \lineii{\e N\{\var{name}\}} |
|
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
382 {Character named \var{name} in the Unicode database (Unicode only)} |
|
8752
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
383 \lineii{\e r} {\ASCII{} Carriage Return (CR)} |
|
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
384 \lineii{\e t} {\ASCII{} Horizontal Tab (TAB)} |
|
19257
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
385 \lineii{\e u\var{xxxx}} {Character with 16-bit hex value \var{xxxx} (Unicode only)} |
|
1c39b96d29c3
Add material about the "r" and "u" prefixes for string literals; should
Fred Drake <fdrake@acm.org>
parents:
19054
diff
changeset
|
386 \lineii{\e U\var{xxxxxxxx}}{Character with 32-bit hex value \var{xxxxxxxx} (Unicode only)} |
|
8752
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
387 \lineii{\e v} {\ASCII{} Vertical Tab (VT)} |
|
15619
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
388 \lineii{\e\var{ooo}} {\ASCII{} character with octal value \var{ooo}} |
|
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
389 \lineii{\e x\var{hh}} {\ASCII{} character with hex value \var{hh}} |
|
8752
4e05e38445ce
Converted some {tabular}s to use {tablei*} environments.
Fred Drake <fdrake@acm.org>
parents:
8736
diff
changeset
|
390 \end{tableii} |
|
21062
bc02c3b09626
Clean up some markup cruft. A number of the macros that take no
Fred Drake <fdrake@acm.org>
parents:
19621
diff
changeset
|
391 \index{ASCII@\ASCII} |
| 8231 | 392 |
|
16591
df7d52ae20e4
Feeble attempt to repair obsolete info about hex escapes.
Tim Peters <tim.peters@gmail.com>
parents:
15776
diff
changeset
|
393 As in Standard C, up to three octal digits are accepted. However, |
|
df7d52ae20e4
Feeble attempt to repair obsolete info about hex escapes.
Tim Peters <tim.peters@gmail.com>
parents:
15776
diff
changeset
|
394 exactly two hex digits are taken in hex escapes. |
| 8231 | 395 |
|
15619
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
396 Unlike Standard \index{unrecognized escape sequence}C, |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
397 all unrecognized escape sequences are left in the string unchanged, |
|
15619
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
398 i.e., \emph{the backslash is left in the string}. (This behavior is |
| 8231 | 399 useful when debugging: if an escape sequence is mistyped, the |
|
15619
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
400 resulting output is more easily recognized as broken.) It is also |
|
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
401 important to note that the escape sequences marked as ``(Unicode |
|
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
402 only)'' in the table above fall into the category of unrecognized |
|
50d9b590627d
Updated string literals description to encompass Unicode literals and the
Fred Drake <fdrake@acm.org>
parents:
15415
diff
changeset
|
403 escapes for non-Unicode string literals. |
| 8231 | 404 |
|
15776
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
405 When an `r' or `R' prefix is present, a character following a |
|
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
406 backslash is included in the string without change, and \emph{all |
|
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
407 backslashes are left in the string}. For example, the string literal |
|
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
408 \code{r"\e n"} consists of two characters: a backslash and a lowercase |
|
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
409 `n'. String quotes can be escaped with a backslash, but the backslash |
|
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
410 remains in the string; for example, \code{r"\e""} is a valid string |
|
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
411 literal consisting of two characters: a backslash and a double quote; |
|
18768
2e173da11d2d
Fix typo in description of raw strings: "value" --> "valid"
Fred Drake <fdrake@acm.org>
parents:
18429
diff
changeset
|
412 \code{r"\e"} is not a valid string literal (even a raw string cannot |
|
15776
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
413 end in an odd number of backslashes). Specifically, \emph{a raw |
|
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
414 string cannot end in a single backslash} (since the backslash would |
|
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
415 escape the following quote character). Note also that a single |
|
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
416 backslash followed by a newline is interpreted as those two characters |
|
4370fc19697e
Steve Holden <sholden@holdenweb.com>:
Fred Drake <fdrake@acm.org>
parents:
15619
diff
changeset
|
417 as part of the string, \emph{not} as a line continuation. |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
418 |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
419 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
420 \subsection{String literal concatenation\label{string-catenation}} |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
421 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
422 Multiple adjacent string literals (delimited by whitespace), possibly |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
423 using different quoting conventions, are allowed, and their meaning is |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
424 the same as their concatenation. Thus, \code{"hello" 'world'} is |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
425 equivalent to \code{"helloworld"}. This feature can be used to reduce |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
426 the number of backslashes needed, to split long strings conveniently |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
427 across long lines, or even to add comments to parts of strings, for |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
428 example: |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
429 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
430 \begin{verbatim} |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
431 re.compile("[A-Za-z_]" # letter or underscore |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
432 "[A-Za-z0-9_]*" # letter, digit or underscore |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
433 ) |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
434 \end{verbatim} |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
435 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
436 Note that this feature is defined at the syntactical level, but |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
437 implemented at compile time. The `+' operator must be used to |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
438 concatenate string expressions at run time. Also note that literal |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
439 concatenation can use different quoting styles for each component |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
440 (even mixing raw strings and triple quoted strings). |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
441 |
|
15415
0f00b6c7b9b9
Note that readframes() returns data in linear format, even if the original
Fred Drake <fdrake@acm.org>
parents:
14160
diff
changeset
|
442 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
443 \subsection{Numeric literals\label{numbers}} |
| 8231 | 444 |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
445 There are four types of numeric literals: plain integers, long |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
446 integers, floating point numbers, and imaginary numbers. There are no |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
447 complex literals (complex numbers can be formed by adding a real |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
448 number and an imaginary number). |
| 8231 | 449 \index{number} |
| 450 \index{numeric literal} | |
| 451 \index{integer literal} | |
| 452 \index{plain integer literal} | |
| 453 \index{long integer literal} | |
| 454 \index{floating point literal} | |
| 455 \index{hexadecimal literal} | |
| 456 \index{octal literal} | |
| 457 \index{decimal literal} | |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
458 \index{imaginary literal} |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
459 \index{complex literal} |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
460 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
461 Note that numeric literals do not include a sign; a phrase like |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
462 \code{-1} is actually an expression composed of the unary operator |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
463 `\code{-}' and the literal \code{1}. |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
464 |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
465 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
466 \subsection{Integer and long integer literals\label{integers}} |
| 8231 | 467 |
| 468 Integer and long integer literals are described by the following | |
| 469 lexical definitions: | |
| 470 | |
|
18429
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
471 \begin{productionlist} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
472 \production{longinteger} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
473 {\token{integer} ("l" | "L")} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
474 \production{integer} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
475 {\token{decimalinteger} | \token{octinteger} | \token{hexinteger}} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
476 \production{decimalinteger} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
477 {\token{nonzerodigit} \token{digit}* | "0"} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
478 \production{octinteger} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
479 {"0" \token{octdigit}+} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
480 \production{hexinteger} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
481 {"0" ("x" | "X") \token{hexdigit}+} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
482 \production{nonzerodigit} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
483 {"1"..."9"} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
484 \production{octdigit} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
485 {"0"..."7"} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
486 \production{hexdigit} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
487 {\token{digit} | "a"..."f" | "A"..."F"} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
488 \end{productionlist} |
| 8231 | 489 |
| 490 Although both lower case `l' and upper case `L' are allowed as suffix | |
| 491 for long integers, it is strongly recommended to always use `L', since | |
| 492 the letter `l' looks too much like the digit `1'. | |
| 493 | |
| 494 Plain integer decimal literals must be at most 2147483647 (i.e., the | |
| 495 largest positive integer, using 32-bit arithmetic). Plain octal and | |
| 496 hexadecimal literals may be as large as 4294967295, but values larger | |
| 497 than 2147483647 are converted to a negative value by subtracting | |
| 498 4294967296. There is no limit for long integer literals apart from | |
| 499 what can be stored in available memory. | |
| 500 | |
| 501 Some examples of plain and long integer literals: | |
| 502 | |
| 503 \begin{verbatim} | |
| 504 7 2147483647 0177 0x80000000 | |
| 505 3L 79228162514264337593543950336L 0377L 0x100000000L | |
| 506 \end{verbatim} | |
| 507 | |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
508 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
509 \subsection{Floating point literals\label{floating}} |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
510 |
| 8231 | 511 Floating point literals are described by the following lexical |
| 512 definitions: | |
| 513 | |
|
18429
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
514 \begin{productionlist} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
515 \production{floatnumber} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
516 {\token{pointfloat} | \token{exponentfloat}} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
517 \production{pointfloat} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
518 {[\token{intpart}] \token{fraction} | \token{intpart} "."} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
519 \production{exponentfloat} |
|
19621
0bec699783d0
SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents:
19257
diff
changeset
|
520 {(\token{intpart} | \token{pointfloat}) |
|
18429
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
521 \token{exponent}} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
522 \production{intpart} |
|
19621
0bec699783d0
SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents:
19257
diff
changeset
|
523 {\token{digit}+} |
|
18429
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
524 \production{fraction} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
525 {"." \token{digit}+} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
526 \production{exponent} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
527 {("e" | "E") ["+" | "-"] \token{digit}+} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
528 \end{productionlist} |
| 8231 | 529 |
|
19621
0bec699783d0
SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents:
19257
diff
changeset
|
530 Note that the integer and exponent parts of floating point numbers |
|
0bec699783d0
SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents:
19257
diff
changeset
|
531 can look like octal integers, but are interpreted using radix 10. For |
|
0bec699783d0
SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents:
19257
diff
changeset
|
532 example, \samp{077e010} is legal, and denotes the same number |
|
0bec699783d0
SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents:
19257
diff
changeset
|
533 as \samp{77e10}. |
| 8231 | 534 The allowed range of floating point literals is |
| 535 implementation-dependent. | |
| 536 Some examples of floating point literals: | |
| 537 | |
| 538 \begin{verbatim} | |
|
19621
0bec699783d0
SF patch #455966: Allow leading 0 in float/imag literals.
Tim Peters <tim.peters@gmail.com>
parents:
19257
diff
changeset
|
539 3.14 10. .001 1e100 3.14e-10 0e0 |
| 8231 | 540 \end{verbatim} |
| 541 | |
| 542 Note that numeric literals do not include a sign; a phrase like | |
|
8337
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
543 \code{-1} is actually an expression composed of the operator |
|
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
544 \code{-} and the literal \code{1}. |
| 8231 | 545 |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
546 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
547 \subsection{Imaginary literals\label{imaginary}} |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
548 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
549 Imaginary literals are described by the following lexical definitions: |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
550 |
|
18429
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
551 \begin{productionlist} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
552 \production{imagnumber}{(\token{floatnumber} | \token{intpart}) ("j" | "J")} |
|
9c354e69a489
Change the grammar productions to use the new productionlist environment;
Fred Drake <fdrake@acm.org>
parents:
18245
diff
changeset
|
553 \end{productionlist} |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
554 |
|
12217
5ea773413531
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
10356
diff
changeset
|
555 An imaginary literal yields a complex number with a real part of |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
556 0.0. Complex numbers are represented as a pair of floating point |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
557 numbers and have the same restrictions on their range. To create a |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
558 complex number with a nonzero real part, add a floating point number |
| 8736 | 559 to it, e.g., \code{(3+4j)}. Some examples of imaginary literals: |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
560 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
561 \begin{verbatim} |
| 8736 | 562 3.14j 10.j 10j .001j 1e100j 3.14e-10j |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
563 \end{verbatim} |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
564 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
565 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
566 \section{Operators\label{operators}} |
| 8231 | 567 |
| 568 The following tokens are operators: | |
| 569 \index{operators} | |
| 570 | |
| 571 \begin{verbatim} | |
|
19054
11bba30e9c28
"Thin" version of floor division docs: add // to the list of operators
Fred Drake <fdrake@acm.org>
parents:
18772
diff
changeset
|
572 + - * ** / // % |
| 8231 | 573 << >> & | ^ ~ |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
574 < > <= >= == != <> |
| 8231 | 575 \end{verbatim} |
| 576 | |
|
8337
8d90e781ada8
Updated markup style (got rid of \verb@...@, mostly).
Fred Drake <fdrake@acm.org>
parents:
8231
diff
changeset
|
577 The comparison operators \code{<>} and \code{!=} are alternate |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
578 spellings of the same operator. \code{!=} is the preferred spelling; |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
579 \code{<>} is obsolescent. |
| 8231 | 580 |
|
18236
f8287d84b755
Add "yeild" to the list of keywords.
Fred Drake <fdrake@acm.org>
parents:
16591
diff
changeset
|
581 |
|
8777
00524555ef17
Make sure all chapters, sections, and subsections have a \label to give them
Fred Drake <fdrake@acm.org>
parents:
8752
diff
changeset
|
582 \section{Delimiters\label{delimiters}} |
| 8231 | 583 |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
584 The following tokens serve as delimiters in the grammar: |
| 8231 | 585 \index{delimiters} |
| 586 | |
| 587 \begin{verbatim} | |
| 588 ( ) [ ] { } | |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
589 , : . ` = ; |
|
19054
11bba30e9c28
"Thin" version of floor division docs: add // to the list of operators
Fred Drake <fdrake@acm.org>
parents:
18772
diff
changeset
|
590 += -= *= /= //= %= |
|
11bba30e9c28
"Thin" version of floor division docs: add // to the list of operators
Fred Drake <fdrake@acm.org>
parents:
18772
diff
changeset
|
591 &= |= ^= >>= <<= **= |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
592 \end{verbatim} |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
593 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
594 The period can also occur in floating-point and imaginary literals. A |
|
12217
5ea773413531
Merged changes from the 1.5.2p2 release.
Fred Drake <fdrake@acm.org>
parents:
10356
diff
changeset
|
595 sequence of three periods has a special meaning as an ellipsis in slices. |
|
14160
5468b6e3b96a
Rough and incomplete documentation on augmented assignment, which follows
Thomas Wouters <thomas@python.org>
parents:
12217
diff
changeset
|
596 The second half of the list, the augmented assignment operators, serve |
|
5468b6e3b96a
Rough and incomplete documentation on augmented assignment, which follows
Thomas Wouters <thomas@python.org>
parents:
12217
diff
changeset
|
597 lexically as delimiters, but also perform an operation. |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
598 |
|
21062
bc02c3b09626
Clean up some markup cruft. A number of the macros that take no
Fred Drake <fdrake@acm.org>
parents:
19621
diff
changeset
|
599 The following printing \ASCII{} characters have special meaning as part |
|
8508
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
600 of other tokens or are otherwise significant to the lexical analyzer: |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
601 |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
602 \begin{verbatim} |
|
ded69d12cc68
Lots of changes to get this in sync with the Frame version.
Guido van Rossum <guido@python.org>
parents:
8337
diff
changeset
|
603 ' " # \ |
| 8231 | 604 \end{verbatim} |
| 605 | |
| 606 The following printing \ASCII{} characters are not used in Python. Their | |
| 607 occurrence outside string literals and comments is an unconditional | |
| 608 error: | |
|
21062
bc02c3b09626
Clean up some markup cruft. A number of the macros that take no
Fred Drake <fdrake@acm.org>
parents:
19621
diff
changeset
|
609 \index{ASCII@\ASCII} |
| 8231 | 610 |
| 611 \begin{verbatim} | |
| 612 @ $ ? | |
| 613 \end{verbatim} |
