Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | .. _lexical: |
| 3 | |
| 4 | **************** |
| 5 | Lexical analysis |
| 6 | **************** |
| 7 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 8 | .. index:: lexical analysis, parser, token |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
| 10 | A Python program is read by a *parser*. Input to the parser is a stream of |
| 11 | *tokens*, generated by the *lexical analyzer*. This chapter describes how the |
| 12 | lexical analyzer breaks a file into tokens. |
| 13 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 14 | Python reads program text as Unicode code points; the encoding of a source file |
| 15 | can be given by an encoding declaration and defaults to UTF-8, see :pep:`3120` |
| 16 | for details. If the source file cannot be decoded, a :exc:`SyntaxError` is |
| 17 | raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | .. _line-structure: |
| 21 | |
| 22 | Line structure |
| 23 | ============== |
| 24 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 25 | .. index:: line structure |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
| 27 | A Python program is divided into a number of *logical lines*. |
| 28 | |
| 29 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 30 | .. _logical-lines: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | |
| 32 | Logical lines |
| 33 | ------------- |
| 34 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 35 | .. index:: logical line, physical line, line joining, NEWLINE token |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | |
| 37 | The end of a logical line is represented by the token NEWLINE. Statements |
| 38 | cannot cross logical line boundaries except where NEWLINE is allowed by the |
| 39 | syntax (e.g., between statements in compound statements). A logical line is |
| 40 | constructed from one or more *physical lines* by following the explicit or |
| 41 | implicit *line joining* rules. |
| 42 | |
| 43 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 44 | .. _physical-lines: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | |
| 46 | Physical lines |
| 47 | -------------- |
| 48 | |
| 49 | A physical line is a sequence of characters terminated by an end-of-line |
| 50 | sequence. In source files, any of the standard platform line termination |
| 51 | sequences can be used - the Unix form using ASCII LF (linefeed), the Windows |
Georg Brandl | c575c90 | 2008-09-13 17:46:05 +0000 | [diff] [blame] | 52 | form using the ASCII sequence CR LF (return followed by linefeed), or the old |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | Macintosh form using the ASCII CR (return) character. All of these forms can be |
| 54 | used equally, regardless of platform. |
| 55 | |
| 56 | When embedding Python, source code strings should be passed to Python APIs using |
| 57 | the standard C conventions for newline characters (the ``\n`` character, |
| 58 | representing ASCII LF, is the line terminator). |
| 59 | |
| 60 | |
| 61 | .. _comments: |
| 62 | |
| 63 | Comments |
| 64 | -------- |
| 65 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 66 | .. index:: comment, hash character |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
| 68 | A comment starts with a hash character (``#``) that is not part of a string |
| 69 | literal, and ends at the end of the physical line. A comment signifies the end |
| 70 | of the logical line unless the implicit line joining rules are invoked. Comments |
| 71 | are ignored by the syntax; they are not tokens. |
| 72 | |
| 73 | |
| 74 | .. _encodings: |
| 75 | |
| 76 | Encoding declarations |
| 77 | --------------------- |
| 78 | |
R David Murray | f7f9818 | 2014-04-16 21:48:04 -0400 | [diff] [blame] | 79 | .. index:: source character set, encoding declarations (source file) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
| 81 | If a comment in the first or second line of the Python script matches the |
| 82 | regular expression ``coding[=:]\s*([-\w.]+)``, this comment is processed as an |
| 83 | encoding declaration; the first group of this expression names the encoding of |
Robert Collins | 0b2833e | 2015-08-06 21:08:44 +1200 | [diff] [blame] | 84 | the source code file. The encoding declaration must appear on a line of its |
| 85 | own. If it is the second line, the first line must also be a comment-only line. |
| 86 | The recommended forms of an encoding expression are :: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | |
| 88 | # -*- coding: <encoding-name> -*- |
| 89 | |
| 90 | which is recognized also by GNU Emacs, and :: |
| 91 | |
| 92 | # vim:fileencoding=<encoding-name> |
| 93 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 94 | which is recognized by Bram Moolenaar's VIM. |
| 95 | |
| 96 | If no encoding declaration is found, the default encoding is UTF-8. In |
| 97 | addition, if the first bytes of the file are the UTF-8 byte-order mark |
| 98 | (``b'\xef\xbb\xbf'``), the declared file encoding is UTF-8 (this is supported, |
| 99 | among others, by Microsoft's :program:`notepad`). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
| 101 | If an encoding is declared, the encoding name must be recognized by Python. The |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 102 | encoding is used for all lexical analysis, including string literals, comments |
Robert Collins | 0b2833e | 2015-08-06 21:08:44 +1200 | [diff] [blame] | 103 | and identifiers. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 105 | .. XXX there should be a list of supported encodings. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | .. _explicit-joining: |
| 109 | |
| 110 | Explicit line joining |
| 111 | --------------------- |
| 112 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 113 | .. index:: physical line, line joining, line continuation, backslash character |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | |
| 115 | Two or more physical lines may be joined into logical lines using backslash |
| 116 | characters (``\``), as follows: when a physical line ends in a backslash that is |
| 117 | not part of a string literal or comment, it is joined with the following forming |
| 118 | a single logical line, deleting the backslash and the following end-of-line |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 119 | character. For example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 120 | |
| 121 | if 1900 < year < 2100 and 1 <= month <= 12 \ |
| 122 | and 1 <= day <= 31 and 0 <= hour < 24 \ |
| 123 | and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date |
| 124 | return 1 |
| 125 | |
| 126 | A line ending in a backslash cannot carry a comment. A backslash does not |
| 127 | continue a comment. A backslash does not continue a token except for string |
| 128 | literals (i.e., tokens other than string literals cannot be split across |
| 129 | physical lines using a backslash). A backslash is illegal elsewhere on a line |
| 130 | outside a string literal. |
| 131 | |
| 132 | |
| 133 | .. _implicit-joining: |
| 134 | |
| 135 | Implicit line joining |
| 136 | --------------------- |
| 137 | |
| 138 | Expressions in parentheses, square brackets or curly braces can be split over |
| 139 | more than one physical line without using backslashes. For example:: |
| 140 | |
| 141 | month_names = ['Januari', 'Februari', 'Maart', # These are the |
| 142 | 'April', 'Mei', 'Juni', # Dutch names |
| 143 | 'Juli', 'Augustus', 'September', # for the months |
| 144 | 'Oktober', 'November', 'December'] # of the year |
| 145 | |
| 146 | Implicitly continued lines can carry comments. The indentation of the |
| 147 | continuation lines is not important. Blank continuation lines are allowed. |
| 148 | There is no NEWLINE token between implicit continuation lines. Implicitly |
| 149 | continued lines can also occur within triple-quoted strings (see below); in that |
| 150 | case they cannot carry comments. |
| 151 | |
| 152 | |
| 153 | .. _blank-lines: |
| 154 | |
| 155 | Blank lines |
| 156 | ----------- |
| 157 | |
| 158 | .. index:: single: blank line |
| 159 | |
| 160 | A logical line that contains only spaces, tabs, formfeeds and possibly a |
| 161 | comment, is ignored (i.e., no NEWLINE token is generated). During interactive |
| 162 | input of statements, handling of a blank line may differ depending on the |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 163 | implementation of the read-eval-print loop. In the standard interactive |
| 164 | interpreter, an entirely blank logical line (i.e. one containing not even |
| 165 | whitespace or a comment) terminates a multi-line statement. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 166 | |
| 167 | |
| 168 | .. _indentation: |
| 169 | |
| 170 | Indentation |
| 171 | ----------- |
| 172 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 173 | .. index:: indentation, leading whitespace, space, tab, grouping, statement grouping |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | |
| 175 | Leading whitespace (spaces and tabs) at the beginning of a logical line is used |
| 176 | to compute the indentation level of the line, which in turn is used to determine |
| 177 | the grouping of statements. |
| 178 | |
Georg Brandl | 861ac1f | 2008-12-15 08:43:10 +0000 | [diff] [blame] | 179 | Tabs are replaced (from left to right) by one to eight spaces such that the |
| 180 | total number of characters up to and including the replacement is a multiple of |
| 181 | eight (this is intended to be the same rule as used by Unix). The total number |
| 182 | of spaces preceding the first non-blank character then determines the line's |
| 183 | indentation. Indentation cannot be split over multiple physical lines using |
| 184 | backslashes; the whitespace up to the first backslash determines the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 185 | indentation. |
| 186 | |
Georg Brandl | 861ac1f | 2008-12-15 08:43:10 +0000 | [diff] [blame] | 187 | Indentation is rejected as inconsistent if a source file mixes tabs and spaces |
| 188 | in a way that makes the meaning dependent on the worth of a tab in spaces; a |
| 189 | :exc:`TabError` is raised in that case. |
| 190 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 191 | **Cross-platform compatibility note:** because of the nature of text editors on |
| 192 | non-UNIX platforms, it is unwise to use a mixture of spaces and tabs for the |
| 193 | indentation in a single source file. It should also be noted that different |
| 194 | platforms may explicitly limit the maximum indentation level. |
| 195 | |
| 196 | A formfeed character may be present at the start of the line; it will be ignored |
| 197 | for the indentation calculations above. Formfeed characters occurring elsewhere |
| 198 | in the leading whitespace have an undefined effect (for instance, they may reset |
| 199 | the space count to zero). |
| 200 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 201 | .. index:: INDENT token, DEDENT token |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | |
| 203 | The indentation levels of consecutive lines are used to generate INDENT and |
| 204 | DEDENT tokens, using a stack, as follows. |
| 205 | |
| 206 | Before the first line of the file is read, a single zero is pushed on the stack; |
| 207 | this will never be popped off again. The numbers pushed on the stack will |
| 208 | always be strictly increasing from bottom to top. At the beginning of each |
| 209 | logical line, the line's indentation level is compared to the top of the stack. |
| 210 | If it is equal, nothing happens. If it is larger, it is pushed on the stack, and |
| 211 | one INDENT token is generated. If it is smaller, it *must* be one of the |
| 212 | numbers occurring on the stack; all numbers on the stack that are larger are |
| 213 | popped off, and for each number popped off a DEDENT token is generated. At the |
| 214 | end of the file, a DEDENT token is generated for each number remaining on the |
| 215 | stack that is larger than zero. |
| 216 | |
| 217 | Here is an example of a correctly (though confusingly) indented piece of Python |
| 218 | code:: |
| 219 | |
| 220 | def perm(l): |
| 221 | # Compute the list of all permutations of l |
| 222 | if len(l) <= 1: |
| 223 | return [l] |
| 224 | r = [] |
| 225 | for i in range(len(l)): |
| 226 | s = l[:i] + l[i+1:] |
| 227 | p = perm(s) |
| 228 | for x in p: |
| 229 | r.append(l[i:i+1] + x) |
| 230 | return r |
| 231 | |
| 232 | The following example shows various indentation errors:: |
| 233 | |
| 234 | def perm(l): # error: first line indented |
| 235 | for i in range(len(l)): # error: not indented |
| 236 | s = l[:i] + l[i+1:] |
| 237 | p = perm(l[:i] + l[i+1:]) # error: unexpected indent |
| 238 | for x in p: |
| 239 | r.append(l[i:i+1] + x) |
| 240 | return r # error: inconsistent dedent |
| 241 | |
| 242 | (Actually, the first three errors are detected by the parser; only the last |
| 243 | error is found by the lexical analyzer --- the indentation of ``return r`` does |
| 244 | not match a level popped off the stack.) |
| 245 | |
| 246 | |
| 247 | .. _whitespace: |
| 248 | |
| 249 | Whitespace between tokens |
| 250 | ------------------------- |
| 251 | |
| 252 | Except at the beginning of a logical line or in string literals, the whitespace |
| 253 | characters space, tab and formfeed can be used interchangeably to separate |
| 254 | tokens. Whitespace is needed between two tokens only if their concatenation |
| 255 | could otherwise be interpreted as a different token (e.g., ab is one token, but |
| 256 | a b is two tokens). |
| 257 | |
| 258 | |
| 259 | .. _other-tokens: |
| 260 | |
| 261 | Other tokens |
| 262 | ============ |
| 263 | |
| 264 | Besides NEWLINE, INDENT and DEDENT, the following categories of tokens exist: |
| 265 | *identifiers*, *keywords*, *literals*, *operators*, and *delimiters*. Whitespace |
| 266 | characters (other than line terminators, discussed earlier) are not tokens, but |
| 267 | serve to delimit tokens. Where ambiguity exists, a token comprises the longest |
| 268 | possible string that forms a legal token, when read from left to right. |
| 269 | |
| 270 | |
| 271 | .. _identifiers: |
| 272 | |
| 273 | Identifiers and keywords |
| 274 | ======================== |
| 275 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 276 | .. index:: identifier, name |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 277 | |
| 278 | Identifiers (also referred to as *names*) are described by the following lexical |
Georg Brandl | e06de8b | 2008-05-05 21:42:51 +0000 | [diff] [blame] | 279 | definitions. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 280 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 281 | The syntax of identifiers in Python is based on the Unicode standard annex |
Georg Brandl | e06de8b | 2008-05-05 21:42:51 +0000 | [diff] [blame] | 282 | UAX-31, with elaboration and changes as defined below; see also :pep:`3131` for |
| 283 | further details. |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 284 | |
| 285 | Within the ASCII range (U+0001..U+007F), the valid characters for identifiers |
Georg Brandl | e06de8b | 2008-05-05 21:42:51 +0000 | [diff] [blame] | 286 | are the same as in Python 2.x: the uppercase and lowercase letters ``A`` through |
| 287 | ``Z``, the underscore ``_`` and, except for the first character, the digits |
| 288 | ``0`` through ``9``. |
| 289 | |
| 290 | Python 3.0 introduces additional characters from outside the ASCII range (see |
| 291 | :pep:`3131`). For these characters, the classification uses the version of the |
| 292 | Unicode Character Database as included in the :mod:`unicodedata` module. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 293 | |
| 294 | Identifiers are unlimited in length. Case is significant. |
| 295 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 296 | .. productionlist:: |
Martin v. Löwis | 0dbebc0 | 2010-12-30 08:36:37 +0000 | [diff] [blame] | 297 | identifier: `xid_start` `xid_continue`* |
Mark Summerfield | 051d1dd | 2007-11-20 13:22:19 +0000 | [diff] [blame] | 298 | id_start: <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property> |
| 299 | id_continue: <all characters in `id_start`, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property> |
Martin v. Löwis | 0dbebc0 | 2010-12-30 08:36:37 +0000 | [diff] [blame] | 300 | xid_start: <all characters in `id_start` whose NFKC normalization is in "id_start xid_continue*"> |
| 301 | xid_continue: <all characters in `id_continue` whose NFKC normalization is in "id_continue*"> |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 302 | |
| 303 | The Unicode category codes mentioned above stand for: |
| 304 | |
| 305 | * *Lu* - uppercase letters |
| 306 | * *Ll* - lowercase letters |
| 307 | * *Lt* - titlecase letters |
| 308 | * *Lm* - modifier letters |
| 309 | * *Lo* - other letters |
| 310 | * *Nl* - letter numbers |
| 311 | * *Mn* - nonspacing marks |
| 312 | * *Mc* - spacing combining marks |
| 313 | * *Nd* - decimal numbers |
| 314 | * *Pc* - connector punctuations |
R David Murray | 5f16f90 | 2014-10-09 20:45:59 -0400 | [diff] [blame] | 315 | * *Other_ID_Start* - explicit list of characters in `PropList.txt |
Benjamin Peterson | 4801383 | 2015-06-27 15:45:56 -0500 | [diff] [blame] | 316 | <http://www.unicode.org/Public/8.0.0/ucd/PropList.txt>`_ to support backwards |
R David Murray | 5f16f90 | 2014-10-09 20:45:59 -0400 | [diff] [blame] | 317 | compatibility |
Martin v. Löwis | 0dbebc0 | 2010-12-30 08:36:37 +0000 | [diff] [blame] | 318 | * *Other_ID_Continue* - likewise |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 319 | |
Alexander Belopolsky | 1a7a2e0 | 2010-12-22 01:37:36 +0000 | [diff] [blame] | 320 | All identifiers are converted into the normal form NFKC while parsing; comparison |
| 321 | of identifiers is based on NFKC. |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 322 | |
| 323 | A non-normative HTML file listing all valid identifier characters for Unicode |
| 324 | 4.1 can be found at |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 325 | https://www.dcl.hpi.uni-potsdam.de/home/loewis/table-3131.html. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 326 | |
Mark Summerfield | 051d1dd | 2007-11-20 13:22:19 +0000 | [diff] [blame] | 327 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 328 | .. _keywords: |
| 329 | |
| 330 | Keywords |
| 331 | -------- |
| 332 | |
| 333 | .. index:: |
| 334 | single: keyword |
| 335 | single: reserved word |
| 336 | |
| 337 | The following identifiers are used as reserved words, or *keywords* of the |
| 338 | language, and cannot be used as ordinary identifiers. They must be spelled |
Georg Brandl | 17761d1 | 2009-05-04 20:43:44 +0000 | [diff] [blame] | 339 | exactly as written here: |
| 340 | |
| 341 | .. sourcecode:: text |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 342 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 343 | False class finally is return |
| 344 | None continue for lambda try |
| 345 | True def from nonlocal while |
| 346 | and del global not with |
| 347 | as elif if or yield |
| 348 | assert else import pass |
| 349 | break except in raise |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 350 | |
| 351 | .. _id-classes: |
| 352 | |
| 353 | Reserved classes of identifiers |
| 354 | ------------------------------- |
| 355 | |
| 356 | Certain classes of identifiers (besides keywords) have special meanings. These |
| 357 | classes are identified by the patterns of leading and trailing underscore |
| 358 | characters: |
| 359 | |
| 360 | ``_*`` |
| 361 | Not imported by ``from module import *``. The special identifier ``_`` is used |
| 362 | in the interactive interpreter to store the result of the last evaluation; it is |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 363 | stored in the :mod:`builtins` module. When not in interactive mode, ``_`` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 364 | has no special meaning and is not defined. See section :ref:`import`. |
| 365 | |
| 366 | .. note:: |
| 367 | |
| 368 | The name ``_`` is often used in conjunction with internationalization; |
| 369 | refer to the documentation for the :mod:`gettext` module for more |
| 370 | information on this convention. |
| 371 | |
| 372 | ``__*__`` |
Georg Brandl | 7d180a0 | 2010-08-02 19:32:43 +0000 | [diff] [blame] | 373 | System-defined names. These names are defined by the interpreter and its |
| 374 | implementation (including the standard library). Current system names are |
| 375 | discussed in the :ref:`specialnames` section and elsewhere. More will likely |
| 376 | be defined in future versions of Python. *Any* use of ``__*__`` names, in |
| 377 | any context, that does not follow explicitly documented use, is subject to |
| 378 | breakage without warning. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 379 | |
| 380 | ``__*`` |
| 381 | Class-private names. Names in this category, when used within the context of a |
| 382 | class definition, are re-written to use a mangled form to help avoid name |
| 383 | clashes between "private" attributes of base and derived classes. See section |
| 384 | :ref:`atom-identifiers`. |
| 385 | |
| 386 | |
| 387 | .. _literals: |
| 388 | |
| 389 | Literals |
| 390 | ======== |
| 391 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 392 | .. index:: literal, constant |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 393 | |
| 394 | Literals are notations for constant values of some built-in types. |
| 395 | |
| 396 | |
| 397 | .. _strings: |
| 398 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 399 | String and Bytes literals |
| 400 | ------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 401 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 402 | .. index:: string literal, bytes literal, ASCII |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 403 | |
| 404 | String literals are described by the following lexical definitions: |
| 405 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 406 | .. productionlist:: |
| 407 | stringliteral: [`stringprefix`](`shortstring` | `longstring`) |
Christian Heimes | 0b3847d | 2012-06-20 11:17:58 +0200 | [diff] [blame] | 408 | stringprefix: "r" | "u" | "R" | "U" |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 409 | shortstring: "'" `shortstringitem`* "'" | '"' `shortstringitem`* '"' |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 410 | longstring: "'''" `longstringitem`* "'''" | '"""' `longstringitem`* '"""' |
| 411 | shortstringitem: `shortstringchar` | `stringescapeseq` |
| 412 | longstringitem: `longstringchar` | `stringescapeseq` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 413 | shortstringchar: <any source character except "\" or newline or the quote> |
| 414 | longstringchar: <any source character except "\"> |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 415 | stringescapeseq: "\" <any source character> |
| 416 | |
| 417 | .. productionlist:: |
| 418 | bytesliteral: `bytesprefix`(`shortbytes` | `longbytes`) |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 419 | bytesprefix: "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB" |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 420 | shortbytes: "'" `shortbytesitem`* "'" | '"' `shortbytesitem`* '"' |
| 421 | longbytes: "'''" `longbytesitem`* "'''" | '"""' `longbytesitem`* '"""' |
| 422 | shortbytesitem: `shortbyteschar` | `bytesescapeseq` |
| 423 | longbytesitem: `longbyteschar` | `bytesescapeseq` |
| 424 | shortbyteschar: <any ASCII character except "\" or newline or the quote> |
| 425 | longbyteschar: <any ASCII character except "\"> |
| 426 | bytesescapeseq: "\" <any ASCII character> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 427 | |
| 428 | One syntactic restriction not indicated by these productions is that whitespace |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 429 | is not allowed between the :token:`stringprefix` or :token:`bytesprefix` and the |
| 430 | rest of the literal. The source character set is defined by the encoding |
| 431 | declaration; it is UTF-8 if no encoding declaration is given in the source file; |
| 432 | see section :ref:`encodings`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 433 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 434 | .. index:: triple-quoted string, Unicode Consortium, raw string |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 435 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 436 | In plain English: Both types of literals can be enclosed in matching single quotes |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 437 | (``'``) or double quotes (``"``). They can also be enclosed in matching groups |
| 438 | of three single or double quotes (these are generally referred to as |
| 439 | *triple-quoted strings*). The backslash (``\``) character is used to escape |
| 440 | characters that otherwise have a special meaning, such as newline, backslash |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 441 | itself, or the quote character. |
| 442 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 443 | Bytes literals are always prefixed with ``'b'`` or ``'B'``; they produce an |
| 444 | instance of the :class:`bytes` type instead of the :class:`str` type. They |
| 445 | may only contain ASCII characters; bytes with a numeric value of 128 or greater |
| 446 | must be expressed with escapes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 447 | |
Georg Brandl | a4c8c47 | 2014-10-31 10:38:49 +0100 | [diff] [blame] | 448 | As of Python 3.3 it is possible again to prefix string literals with a |
Armin Ronacher | 50364b4 | 2012-03-04 12:33:51 +0000 | [diff] [blame] | 449 | ``u`` prefix to simplify maintenance of dual 2.x and 3.x codebases. |
| 450 | |
Georg Brandl | 0182f38 | 2012-06-20 11:26:03 +0200 | [diff] [blame] | 451 | Both string and bytes literals may optionally be prefixed with a letter ``'r'`` |
Benjamin Peterson | 162dd74 | 2010-06-29 15:57:57 +0000 | [diff] [blame] | 452 | or ``'R'``; such strings are called :dfn:`raw strings` and treat backslashes as |
| 453 | literal characters. As a result, in string literals, ``'\U'`` and ``'\u'`` |
Christian Heimes | 0b3847d | 2012-06-20 11:17:58 +0200 | [diff] [blame] | 454 | escapes in raw strings are not treated specially. Given that Python 2.x's raw |
| 455 | unicode literals behave differently than Python 3.x's the ``'ur'`` syntax |
| 456 | is not supported. |
Benjamin Peterson | 162dd74 | 2010-06-29 15:57:57 +0000 | [diff] [blame] | 457 | |
Georg Brandl | a4c8c47 | 2014-10-31 10:38:49 +0100 | [diff] [blame] | 458 | .. versionadded:: 3.3 |
| 459 | The ``'rb'`` prefix of raw bytes literals has been added as a synonym |
| 460 | of ``'br'``. |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 461 | |
Georg Brandl | a4c8c47 | 2014-10-31 10:38:49 +0100 | [diff] [blame] | 462 | .. versionadded:: 3.3 |
| 463 | Support for the unicode legacy literal (``u'value'``) was reintroduced |
| 464 | to simplify the maintenance of dual Python 2.x and 3.x codebases. |
| 465 | See :pep:`414` for more information. |
Armin Ronacher | 50364b4 | 2012-03-04 12:33:51 +0000 | [diff] [blame] | 466 | |
Georg Brandl | a4c8c47 | 2014-10-31 10:38:49 +0100 | [diff] [blame] | 467 | In triple-quoted literals, unescaped newlines and quotes are allowed (and are |
| 468 | retained), except that three unescaped quotes in a row terminate the literal. (A |
| 469 | "quote" is the character used to open the literal, i.e. either ``'`` or ``"``.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 470 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 471 | .. index:: physical line, escape sequence, Standard C, C |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 472 | |
Georg Brandl | a4c8c47 | 2014-10-31 10:38:49 +0100 | [diff] [blame] | 473 | Unless an ``'r'`` or ``'R'`` prefix is present, escape sequences in string and |
| 474 | bytes literals are interpreted according to rules similar to those used by |
| 475 | Standard C. The recognized escape sequences are: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 476 | |
| 477 | +-----------------+---------------------------------+-------+ |
| 478 | | Escape Sequence | Meaning | Notes | |
| 479 | +=================+=================================+=======+ |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 480 | | ``\newline`` | Backslash and newline ignored | | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 481 | +-----------------+---------------------------------+-------+ |
| 482 | | ``\\`` | Backslash (``\``) | | |
| 483 | +-----------------+---------------------------------+-------+ |
| 484 | | ``\'`` | Single quote (``'``) | | |
| 485 | +-----------------+---------------------------------+-------+ |
| 486 | | ``\"`` | Double quote (``"``) | | |
| 487 | +-----------------+---------------------------------+-------+ |
| 488 | | ``\a`` | ASCII Bell (BEL) | | |
| 489 | +-----------------+---------------------------------+-------+ |
| 490 | | ``\b`` | ASCII Backspace (BS) | | |
| 491 | +-----------------+---------------------------------+-------+ |
| 492 | | ``\f`` | ASCII Formfeed (FF) | | |
| 493 | +-----------------+---------------------------------+-------+ |
| 494 | | ``\n`` | ASCII Linefeed (LF) | | |
| 495 | +-----------------+---------------------------------+-------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 496 | | ``\r`` | ASCII Carriage Return (CR) | | |
| 497 | +-----------------+---------------------------------+-------+ |
| 498 | | ``\t`` | ASCII Horizontal Tab (TAB) | | |
| 499 | +-----------------+---------------------------------+-------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 500 | | ``\v`` | ASCII Vertical Tab (VT) | | |
| 501 | +-----------------+---------------------------------+-------+ |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 502 | | ``\ooo`` | Character with octal value | (1,3) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 503 | | | *ooo* | | |
| 504 | +-----------------+---------------------------------+-------+ |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 505 | | ``\xhh`` | Character with hex value *hh* | (2,3) | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 506 | +-----------------+---------------------------------+-------+ |
| 507 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 508 | Escape sequences only recognized in string literals are: |
| 509 | |
| 510 | +-----------------+---------------------------------+-------+ |
| 511 | | Escape Sequence | Meaning | Notes | |
| 512 | +=================+=================================+=======+ |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 513 | | ``\N{name}`` | Character named *name* in the | \(4) | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 514 | | | Unicode database | | |
| 515 | +-----------------+---------------------------------+-------+ |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 516 | | ``\uxxxx`` | Character with 16-bit hex value | \(5) | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 517 | | | *xxxx* | | |
| 518 | +-----------------+---------------------------------+-------+ |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 519 | | ``\Uxxxxxxxx`` | Character with 32-bit hex value | \(6) | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 520 | | | *xxxxxxxx* | | |
| 521 | +-----------------+---------------------------------+-------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 522 | |
| 523 | Notes: |
| 524 | |
| 525 | (1) |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 526 | As in Standard C, up to three octal digits are accepted. |
| 527 | |
| 528 | (2) |
Florent Xicluna | 4e0f891 | 2010-03-15 13:14:39 +0000 | [diff] [blame] | 529 | Unlike in Standard C, exactly two hex digits are required. |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 530 | |
| 531 | (3) |
| 532 | In a bytes literal, hexadecimal and octal escapes denote the byte with the |
| 533 | given value. In a string literal, these escapes denote a Unicode character |
| 534 | with the given value. |
| 535 | |
| 536 | (4) |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 537 | .. versionchanged:: 3.3 |
| 538 | Support for name aliases [#]_ has been added. |
| 539 | |
| 540 | (5) |
Berker Peksag | 4f35d79 | 2016-04-24 03:13:40 +0300 | [diff] [blame] | 541 | Exactly four hex digits are required. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 542 | |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 543 | (6) |
Ezio Melotti | e7f9037 | 2012-10-05 03:33:31 +0300 | [diff] [blame] | 544 | Any Unicode character can be encoded this way. Exactly eight hex digits |
Georg Brandl | e43baab | 2010-05-10 21:17:00 +0000 | [diff] [blame] | 545 | are required. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 546 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 547 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 548 | .. index:: unrecognized escape sequence |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 549 | |
| 550 | Unlike Standard C, all unrecognized escape sequences are left in the string |
Georg Brandl | a4c8c47 | 2014-10-31 10:38:49 +0100 | [diff] [blame] | 551 | unchanged, i.e., *the backslash is left in the result*. (This behavior is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 552 | useful when debugging: if an escape sequence is mistyped, the resulting output |
| 553 | is more easily recognized as broken.) It is also important to note that the |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 554 | escape sequences only recognized in string literals fall into the category of |
| 555 | unrecognized escapes for bytes literals. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 556 | |
Georg Brandl | a4c8c47 | 2014-10-31 10:38:49 +0100 | [diff] [blame] | 557 | Even in a raw literal, quotes can be escaped with a backslash, but the |
| 558 | backslash remains in the result; for example, ``r"\""`` is a valid string |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 559 | literal consisting of two characters: a backslash and a double quote; ``r"\"`` |
| 560 | is not a valid string literal (even a raw string cannot end in an odd number of |
Georg Brandl | a4c8c47 | 2014-10-31 10:38:49 +0100 | [diff] [blame] | 561 | backslashes). Specifically, *a raw literal cannot end in a single backslash* |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 562 | (since the backslash would escape the following quote character). Note also |
| 563 | that a single backslash followed by a newline is interpreted as those two |
Georg Brandl | a4c8c47 | 2014-10-31 10:38:49 +0100 | [diff] [blame] | 564 | characters as part of the literal, *not* as a line continuation. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 565 | |
| 566 | |
| 567 | .. _string-catenation: |
| 568 | |
| 569 | String literal concatenation |
| 570 | ---------------------------- |
| 571 | |
Benjamin Peterson | 162dd74 | 2010-06-29 15:57:57 +0000 | [diff] [blame] | 572 | Multiple adjacent string or bytes literals (delimited by whitespace), possibly |
| 573 | using different quoting conventions, are allowed, and their meaning is the same |
| 574 | as their concatenation. Thus, ``"hello" 'world'`` is equivalent to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 575 | ``"helloworld"``. This feature can be used to reduce the number of backslashes |
| 576 | needed, to split long strings conveniently across long lines, or even to add |
| 577 | comments to parts of strings, for example:: |
| 578 | |
| 579 | re.compile("[A-Za-z_]" # letter or underscore |
| 580 | "[A-Za-z0-9_]*" # letter, digit or underscore |
| 581 | ) |
| 582 | |
| 583 | Note that this feature is defined at the syntactical level, but implemented at |
| 584 | compile time. The '+' operator must be used to concatenate string expressions |
| 585 | at run time. Also note that literal concatenation can use different quoting |
| 586 | styles for each component (even mixing raw strings and triple quoted strings). |
| 587 | |
| 588 | |
| 589 | .. _numbers: |
| 590 | |
| 591 | Numeric literals |
| 592 | ---------------- |
| 593 | |
Georg Brandl | ba956ae | 2007-11-29 17:24:34 +0000 | [diff] [blame] | 594 | .. index:: number, numeric literal, integer literal |
| 595 | floating point literal, hexadecimal literal |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 596 | octal literal, binary literal, decimal literal, imaginary literal, complex literal |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 597 | |
Georg Brandl | 95817b3 | 2008-05-11 14:30:18 +0000 | [diff] [blame] | 598 | There are three types of numeric literals: integers, floating point numbers, and |
| 599 | imaginary numbers. There are no complex literals (complex numbers can be formed |
| 600 | by adding a real number and an imaginary number). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 601 | |
| 602 | Note that numeric literals do not include a sign; a phrase like ``-1`` is |
| 603 | actually an expression composed of the unary operator '``-``' and the literal |
| 604 | ``1``. |
| 605 | |
| 606 | |
| 607 | .. _integers: |
| 608 | |
| 609 | Integer literals |
| 610 | ---------------- |
| 611 | |
| 612 | Integer literals are described by the following lexical definitions: |
| 613 | |
| 614 | .. productionlist:: |
Georg Brandl | ddee308 | 2008-04-09 18:46:46 +0000 | [diff] [blame] | 615 | integer: `decimalinteger` | `octinteger` | `hexinteger` | `bininteger` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 616 | decimalinteger: `nonzerodigit` `digit`* | "0"+ |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 617 | nonzerodigit: "1"..."9" |
| 618 | digit: "0"..."9" |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 619 | octinteger: "0" ("o" | "O") `octdigit`+ |
| 620 | hexinteger: "0" ("x" | "X") `hexdigit`+ |
| 621 | bininteger: "0" ("b" | "B") `bindigit`+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 622 | octdigit: "0"..."7" |
| 623 | hexdigit: `digit` | "a"..."f" | "A"..."F" |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 624 | bindigit: "0" | "1" |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 625 | |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 626 | There is no limit for the length of integer literals apart from what can be |
| 627 | stored in available memory. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 628 | |
| 629 | Note that leading zeros in a non-zero decimal number are not allowed. This is |
| 630 | for disambiguation with C-style octal literals, which Python used before version |
| 631 | 3.0. |
| 632 | |
| 633 | Some examples of integer literals:: |
| 634 | |
| 635 | 7 2147483647 0o177 0b100110111 |
Raymond Hettinger | 9ecf9e2 | 2015-05-22 16:37:49 -0700 | [diff] [blame] | 636 | 3 79228162514264337593543950336 0o377 0xdeadbeef |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 637 | |
| 638 | |
| 639 | .. _floating: |
| 640 | |
| 641 | Floating point literals |
| 642 | ----------------------- |
| 643 | |
| 644 | Floating point literals are described by the following lexical definitions: |
| 645 | |
| 646 | .. productionlist:: |
| 647 | floatnumber: `pointfloat` | `exponentfloat` |
| 648 | pointfloat: [`intpart`] `fraction` | `intpart` "." |
| 649 | exponentfloat: (`intpart` | `pointfloat`) `exponent` |
| 650 | intpart: `digit`+ |
| 651 | fraction: "." `digit`+ |
| 652 | exponent: ("e" | "E") ["+" | "-"] `digit`+ |
| 653 | |
| 654 | Note that the integer and exponent parts are always interpreted using radix 10. |
| 655 | For example, ``077e010`` is legal, and denotes the same number as ``77e10``. The |
| 656 | allowed range of floating point literals is implementation-dependent. Some |
| 657 | examples of floating point literals:: |
| 658 | |
| 659 | 3.14 10. .001 1e100 3.14e-10 0e0 |
| 660 | |
| 661 | Note that numeric literals do not include a sign; a phrase like ``-1`` is |
| 662 | actually an expression composed of the unary operator ``-`` and the literal |
| 663 | ``1``. |
| 664 | |
| 665 | |
| 666 | .. _imaginary: |
| 667 | |
| 668 | Imaginary literals |
| 669 | ------------------ |
| 670 | |
| 671 | Imaginary literals are described by the following lexical definitions: |
| 672 | |
| 673 | .. productionlist:: |
| 674 | imagnumber: (`floatnumber` | `intpart`) ("j" | "J") |
| 675 | |
| 676 | An imaginary literal yields a complex number with a real part of 0.0. Complex |
| 677 | numbers are represented as a pair of floating point numbers and have the same |
| 678 | restrictions on their range. To create a complex number with a nonzero real |
| 679 | part, add a floating point number to it, e.g., ``(3+4j)``. Some examples of |
| 680 | imaginary literals:: |
| 681 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 682 | 3.14j 10.j 10j .001j 1e100j 3.14e-10j |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 683 | |
| 684 | |
| 685 | .. _operators: |
| 686 | |
| 687 | Operators |
| 688 | ========= |
| 689 | |
| 690 | .. index:: single: operators |
| 691 | |
| 692 | The following tokens are operators:: |
| 693 | |
Benjamin Peterson | bd59241 | 2014-08-06 22:50:30 -0700 | [diff] [blame] | 694 | + - * ** / // % @ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 695 | << >> & | ^ ~ |
| 696 | < > <= >= == != |
| 697 | |
| 698 | |
| 699 | .. _delimiters: |
| 700 | |
| 701 | Delimiters |
| 702 | ========== |
| 703 | |
| 704 | .. index:: single: delimiters |
| 705 | |
| 706 | The following tokens serve as delimiters in the grammar:: |
| 707 | |
Georg Brandl | 0df7979 | 2008-10-04 18:33:26 +0000 | [diff] [blame] | 708 | ( ) [ ] { } |
Georg Brandl | 97f9623 | 2013-10-08 21:28:22 +0200 | [diff] [blame] | 709 | , : . ; @ = -> |
Benjamin Peterson | bd59241 | 2014-08-06 22:50:30 -0700 | [diff] [blame] | 710 | += -= *= /= //= %= @= |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 711 | &= |= ^= >>= <<= **= |
| 712 | |
| 713 | The period can also occur in floating-point and imaginary literals. A sequence |
Georg Brandl | 57e3b68 | 2007-08-31 08:07:45 +0000 | [diff] [blame] | 714 | of three periods has a special meaning as an ellipsis literal. The second half |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 715 | of the list, the augmented assignment operators, serve lexically as delimiters, |
| 716 | but also perform an operation. |
| 717 | |
| 718 | The following printing ASCII characters have special meaning as part of other |
| 719 | tokens or are otherwise significant to the lexical analyzer:: |
| 720 | |
| 721 | ' " # \ |
| 722 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 723 | The following printing ASCII characters are not used in Python. Their |
| 724 | occurrence outside string literals and comments is an unconditional error:: |
| 725 | |
Georg Brandl | e43baab | 2010-05-10 21:17:00 +0000 | [diff] [blame] | 726 | $ ? ` |
Ezio Melotti | 931b8aa | 2011-10-21 21:57:36 +0300 | [diff] [blame] | 727 | |
| 728 | |
| 729 | .. rubric:: Footnotes |
| 730 | |
Benjamin Peterson | 4801383 | 2015-06-27 15:45:56 -0500 | [diff] [blame] | 731 | .. [#] http://www.unicode.org/Public/8.0.0/ucd/NameAliases.txt |