• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2.. _lexical:
3
4****************
5Lexical analysis
6****************
7
8.. index::
9   single: lexical analysis
10   single: parser
11   single: token
12
13A Python program is read by a *parser*.  Input to the parser is a stream of
14*tokens*, generated by the *lexical analyzer*.  This chapter describes how the
15lexical analyzer breaks a file into tokens.
16
17Python uses the 7-bit ASCII character set for program text.
18
19.. versionadded:: 2.3
20   An encoding declaration can be used to indicate that  string literals and
21   comments use an encoding different from ASCII.
22
23For compatibility with older versions, Python only warns if it finds 8-bit
24characters; those warnings should be corrected by either declaring an explicit
25encoding, or using escape sequences if those bytes are binary data, instead of
26characters.
27
28The run-time character set depends on the I/O devices connected to the program
29but is generally a superset of ASCII.
30
31**Future compatibility note:** It may be tempting to assume that the character
32set for 8-bit characters is ISO Latin-1 (an ASCII superset that covers most
33western languages that use the Latin alphabet), but it is possible that in the
34future Unicode text editors will become common.  These generally use the UTF-8
35encoding, which is also an ASCII superset, but with very different use for the
36characters with ordinals 128-255.  While there is no consensus on this subject
37yet, it is unwise to assume either Latin-1 or UTF-8, even though the current
38implementation appears to favor Latin-1.  This applies both to the source
39character set and the run-time character set.
40
41
42.. _line-structure:
43
44Line structure
45==============
46
47.. index:: single: line structure
48
49A Python program is divided into a number of *logical lines*.
50
51
52.. _logical:
53
54Logical lines
55-------------
56
57.. index::
58   single: logical line
59   single: physical line
60   single: line joining
61   single: NEWLINE token
62
63The end of a logical line is represented by the token NEWLINE.  Statements
64cannot cross logical line boundaries except where NEWLINE is allowed by the
65syntax (e.g., between statements in compound statements). A logical line is
66constructed from one or more *physical lines* by following the explicit or
67implicit *line joining* rules.
68
69
70.. _physical:
71
72Physical lines
73--------------
74
75A physical line is a sequence of characters terminated by an end-of-line
76sequence.  In source files, any of the standard platform line termination
77sequences can be used - the Unix form using ASCII LF (linefeed), the Windows
78form using the ASCII sequence CR LF (return followed by linefeed), or the old
79Macintosh form using the ASCII CR (return) character.  All of these forms can be
80used equally, regardless of platform.
81
82When embedding Python, source code strings should be passed to Python APIs using
83the standard C conventions for newline characters (the ``\n`` character,
84representing ASCII LF, is the line terminator).
85
86
87.. _comments:
88
89Comments
90--------
91
92.. index::
93   single: comment
94   single: hash character
95
96A comment starts with a hash character (``#``) that is not part of a string
97literal, and ends at the end of the physical line.  A comment signifies the end
98of the logical line unless the implicit line joining rules are invoked. Comments
99are ignored by the syntax; they are not tokens.
100
101
102.. _encodings:
103
104Encoding declarations
105---------------------
106
107.. index:: source character set, encoding declarations (source file)
108
109If a comment in the first or second line of the Python script matches the
110regular expression ``coding[=:]\s*([-\w.]+)``, this comment is processed as an
111encoding declaration; the first group of this expression names the encoding of
112the source code file. The encoding declaration must appear on a line of its
113own. If it is the second line, the first line must also be a comment-only line.
114The recommended forms of an encoding expression are ::
115
116   # -*- coding: <encoding-name> -*-
117
118which is recognized also by GNU Emacs, and ::
119
120   # vim:fileencoding=<encoding-name>
121
122which is recognized by Bram Moolenaar's VIM. In addition, if the first bytes of
123the file are the UTF-8 byte-order mark (``'\xef\xbb\xbf'``), the declared file
124encoding is UTF-8 (this is supported, among others, by Microsoft's
125:program:`notepad`).
126
127If an encoding is declared, the encoding name must be recognized by Python. The
128encoding is used for all lexical analysis, in particular to find the end of a
129string, and to interpret the contents of Unicode literals. String literals are
130converted to Unicode for syntactical analysis, then converted back to their
131original encoding before interpretation starts.
132
133.. XXX there should be a list of supported encodings.
134
135
136.. _explicit-joining:
137
138Explicit line joining
139---------------------
140
141.. index::
142   single: physical line
143   single: line joining
144   single: line continuation
145   single: backslash character
146
147Two or more physical lines may be joined into logical lines using backslash
148characters (``\``), as follows: when a physical line ends in a backslash that is
149not part of a string literal or comment, it is joined with the following forming
150a single logical line, deleting the backslash and the following end-of-line
151character.  For example::
152
153   if 1900 < year < 2100 and 1 <= month <= 12 \
154      and 1 <= day <= 31 and 0 <= hour < 24 \
155      and 0 <= minute < 60 and 0 <= second < 60:   # Looks like a valid date
156           return 1
157
158A line ending in a backslash cannot carry a comment.  A backslash does not
159continue a comment.  A backslash does not continue a token except for string
160literals (i.e., tokens other than string literals cannot be split across
161physical lines using a backslash).  A backslash is illegal elsewhere on a line
162outside a string literal.
163
164
165.. _implicit-joining:
166
167Implicit line joining
168---------------------
169
170Expressions in parentheses, square brackets or curly braces can be split over
171more than one physical line without using backslashes. For example::
172
173   month_names = ['Januari', 'Februari', 'Maart',      # These are the
174                  'April',   'Mei',      'Juni',       # Dutch names
175                  'Juli',    'Augustus', 'September',  # for the months
176                  'Oktober', 'November', 'December']   # of the year
177
178Implicitly continued lines can carry comments.  The indentation of the
179continuation lines is not important.  Blank continuation lines are allowed.
180There is no NEWLINE token between implicit continuation lines.  Implicitly
181continued lines can also occur within triple-quoted strings (see below); in that
182case they cannot carry comments.
183
184
185.. _blank-lines:
186
187Blank lines
188-----------
189
190.. index:: single: blank line
191
192A logical line that contains only spaces, tabs, formfeeds and possibly a
193comment, is ignored (i.e., no NEWLINE token is generated).  During interactive
194input of statements, handling of a blank line may differ depending on the
195implementation of the read-eval-print loop.  In the standard implementation, an
196entirely blank logical line (i.e. one containing not even whitespace or a
197comment) terminates a multi-line statement.
198
199
200.. _indentation:
201
202Indentation
203-----------
204
205.. index::
206   single: indentation
207   single: whitespace
208   single: leading whitespace
209   single: space
210   single: tab
211   single: grouping
212   single: statement grouping
213
214Leading whitespace (spaces and tabs) at the beginning of a logical line is used
215to compute the indentation level of the line, which in turn is used to determine
216the grouping of statements.
217
218First, tabs are replaced (from left to right) by one to eight spaces such that
219the total number of characters up to and including the replacement is a multiple
220of eight (this is intended to be the same rule as used by Unix).  The total
221number of spaces preceding the first non-blank character then determines the
222line's indentation.  Indentation cannot be split over multiple physical lines
223using backslashes; the whitespace up to the first backslash determines the
224indentation.
225
226**Cross-platform compatibility note:** because of the nature of text editors on
227non-UNIX platforms, it is unwise to use a mixture of spaces and tabs for the
228indentation in a single source file.  It should also be noted that different
229platforms may explicitly limit the maximum indentation level.
230
231A formfeed character may be present at the start of the line; it will be ignored
232for the indentation calculations above.  Formfeed characters occurring elsewhere
233in the leading whitespace have an undefined effect (for instance, they may reset
234the space count to zero).
235
236.. index::
237   single: INDENT token
238   single: DEDENT token
239
240The indentation levels of consecutive lines are used to generate INDENT and
241DEDENT tokens, using a stack, as follows.
242
243Before the first line of the file is read, a single zero is pushed on the stack;
244this will never be popped off again.  The numbers pushed on the stack will
245always be strictly increasing from bottom to top.  At the beginning of each
246logical line, the line's indentation level is compared to the top of the stack.
247If it is equal, nothing happens. If it is larger, it is pushed on the stack, and
248one INDENT token is generated.  If it is smaller, it *must* be one of the
249numbers occurring on the stack; all numbers on the stack that are larger are
250popped off, and for each number popped off a DEDENT token is generated.  At the
251end of the file, a DEDENT token is generated for each number remaining on the
252stack that is larger than zero.
253
254Here is an example of a correctly (though confusingly) indented piece of Python
255code::
256
257   def perm(l):
258           # Compute the list of all permutations of l
259       if len(l) <= 1:
260                     return [l]
261       r = []
262       for i in range(len(l)):
263                s = l[:i] + l[i+1:]
264                p = perm(s)
265                for x in p:
266                 r.append(l[i:i+1] + x)
267       return r
268
269The following example shows various indentation errors::
270
271    def perm(l):                       # error: first line indented
272   for i in range(len(l)):             # error: not indented
273       s = l[:i] + l[i+1:]
274           p = perm(l[:i] + l[i+1:])   # error: unexpected indent
275           for x in p:
276                   r.append(l[i:i+1] + x)
277               return r                # error: inconsistent dedent
278
279(Actually, the first three errors are detected by the parser; only the last
280error is found by the lexical analyzer --- the indentation of ``return r`` does
281not match a level popped off the stack.)
282
283
284.. _whitespace:
285
286Whitespace between tokens
287-------------------------
288
289Except at the beginning of a logical line or in string literals, the whitespace
290characters space, tab and formfeed can be used interchangeably to separate
291tokens.  Whitespace is needed between two tokens only if their concatenation
292could otherwise be interpreted as a different token (e.g., ab is one token, but
293a b is two tokens).
294
295
296.. _other-tokens:
297
298Other tokens
299============
300
301Besides NEWLINE, INDENT and DEDENT, the following categories of tokens exist:
302*identifiers*, *keywords*, *literals*, *operators*, and *delimiters*. Whitespace
303characters (other than line terminators, discussed earlier) are not tokens, but
304serve to delimit tokens. Where ambiguity exists, a token comprises the longest
305possible string that forms a legal token, when read from left to right.
306
307
308.. _identifiers:
309
310Identifiers and keywords
311========================
312
313.. index::
314   single: identifier
315   single: name
316
317Identifiers (also referred to as *names*) are described by the following lexical
318definitions:
319
320.. productionlist::
321   identifier: (`letter`|"_") (`letter` | `digit` | "_")*
322   letter: `lowercase` | `uppercase`
323   lowercase: "a"..."z"
324   uppercase: "A"..."Z"
325   digit: "0"..."9"
326
327Identifiers are unlimited in length.  Case is significant.
328
329
330.. _keywords:
331
332Keywords
333--------
334
335.. index::
336   single: keyword
337   single: reserved word
338
339The following identifiers are used as reserved words, or *keywords* of the
340language, and cannot be used as ordinary identifiers.  They must be spelled
341exactly as written here:
342
343.. sourcecode:: text
344
345   and       del       from      not       while
346   as        elif      global    or        with
347   assert    else      if        pass      yield
348   break     except    import    print
349   class     exec      in        raise
350   continue  finally   is        return
351   def       for       lambda    try
352
353.. versionchanged:: 2.4
354   :const:`None` became a constant and is now recognized by the compiler as a name
355   for the built-in object :const:`None`.  Although it is not a keyword, you cannot
356   assign a different object to it.
357
358.. versionchanged:: 2.5
359   Using :keyword:`as` and :keyword:`with` as identifiers triggers a warning.  To
360   use them as keywords, enable the ``with_statement`` future feature .
361
362.. versionchanged:: 2.6
363    :keyword:`as` and :keyword:`with` are full keywords.
364
365
366.. _id-classes:
367
368Reserved classes of identifiers
369-------------------------------
370
371Certain classes of identifiers (besides keywords) have special meanings.  These
372classes are identified by the patterns of leading and trailing underscore
373characters:
374
375``_*``
376   Not imported by ``from module import *``.  The special identifier ``_`` is used
377   in the interactive interpreter to store the result of the last evaluation; it is
378   stored in the :mod:`__builtin__` module.  When not in interactive mode, ``_``
379   has no special meaning and is not defined. See section :ref:`import`.
380
381   .. note::
382
383      The name ``_`` is often used in conjunction with internationalization;
384      refer to the documentation for the :mod:`gettext` module for more
385      information on this convention.
386
387``__*__``
388   System-defined names. These names are defined by the interpreter and its
389   implementation (including the standard library).  Current system names are
390   discussed in the :ref:`specialnames` section and elsewhere.  More will likely
391   be defined in future versions of Python.  *Any* use of ``__*__`` names, in
392   any context, that does not follow explicitly documented use, is subject to
393   breakage without warning.
394
395``__*``
396   Class-private names.  Names in this category, when used within the context of a
397   class definition, are re-written to use a mangled form to help avoid name
398   clashes between "private" attributes of base and derived classes. See section
399   :ref:`atom-identifiers`.
400
401
402.. _literals:
403
404Literals
405========
406
407.. index::
408   single: literal
409   single: constant
410
411Literals are notations for constant values of some built-in types.
412
413
414.. _strings:
415
416String literals
417---------------
418
419.. index:: single: string literal
420
421String literals are described by the following lexical definitions:
422
423.. index:: single: ASCII@ASCII
424
425.. productionlist::
426   stringliteral: [`stringprefix`](`shortstring` | `longstring`)
427   stringprefix: "r" | "u" | "ur" | "R" | "U" | "UR" | "Ur" | "uR"
428               : | "b" | "B" | "br" | "Br" | "bR" | "BR"
429   shortstring: "'" `shortstringitem`* "'" | '"' `shortstringitem`* '"'
430   longstring: "'''" `longstringitem`* "'''"
431             : | '"""' `longstringitem`* '"""'
432   shortstringitem: `shortstringchar` | `escapeseq`
433   longstringitem: `longstringchar` | `escapeseq`
434   shortstringchar: <any source character except "\" or newline or the quote>
435   longstringchar: <any source character except "\">
436   escapeseq: "\" <any ASCII character>
437
438One syntactic restriction not indicated by these productions is that whitespace
439is not allowed between the :token:`stringprefix` and the rest of the string
440literal. The source character set is defined by the encoding declaration; it is
441ASCII if no encoding declaration is given in the source file; see section
442:ref:`encodings`.
443
444.. index::
445   single: triple-quoted string
446   single: Unicode Consortium
447   single: string; Unicode
448   single: raw string
449
450In plain English: String literals can be enclosed in matching single quotes
451(``'``) or double quotes (``"``).  They can also be enclosed in matching groups
452of three single or double quotes (these are generally referred to as
453*triple-quoted strings*).  The backslash (``\``) character is used to escape
454characters that otherwise have a special meaning, such as newline, backslash
455itself, or the quote character.  String literals may optionally be prefixed with
456a letter ``'r'`` or ``'R'``; such strings are called :dfn:`raw strings` and use
457different rules for interpreting backslash escape sequences.  A prefix of
458``'u'`` or ``'U'`` makes the string a Unicode string.  Unicode strings use the
459Unicode character set as defined by the Unicode Consortium and ISO 10646.  Some
460additional escape sequences, described below, are available in Unicode strings.
461A prefix of ``'b'`` or ``'B'`` is ignored in Python 2; it indicates that the
462literal should become a bytes literal in Python 3 (e.g. when code is
463automatically converted with 2to3).  A ``'u'`` or ``'b'`` prefix may be followed
464by an ``'r'`` prefix.
465
466In triple-quoted strings, unescaped newlines and quotes are allowed (and are
467retained), except that three unescaped quotes in a row terminate the string.  (A
468"quote" is the character used to open the string, i.e. either ``'`` or ``"``.)
469
470.. index::
471   single: physical line
472   single: escape sequence
473   single: Standard C
474   single: C
475
476Unless an ``'r'`` or ``'R'`` prefix is present, escape sequences in strings are
477interpreted according to rules similar to those used by Standard C.  The
478recognized escape sequences are:
479
480+-----------------+---------------------------------+-------+
481| Escape Sequence | Meaning                         | Notes |
482+=================+=================================+=======+
483| ``\newline``    | Ignored                         |       |
484+-----------------+---------------------------------+-------+
485| ``\\``          | Backslash (``\``)               |       |
486+-----------------+---------------------------------+-------+
487| ``\'``          | Single quote (``'``)            |       |
488+-----------------+---------------------------------+-------+
489| ``\"``          | Double quote (``"``)            |       |
490+-----------------+---------------------------------+-------+
491| ``\a``          | ASCII Bell (BEL)                |       |
492+-----------------+---------------------------------+-------+
493| ``\b``          | ASCII Backspace (BS)            |       |
494+-----------------+---------------------------------+-------+
495| ``\f``          | ASCII Formfeed (FF)             |       |
496+-----------------+---------------------------------+-------+
497| ``\n``          | ASCII Linefeed (LF)             |       |
498+-----------------+---------------------------------+-------+
499| ``\N{name}``    | Character named *name* in the   |       |
500|                 | Unicode database (Unicode only) |       |
501+-----------------+---------------------------------+-------+
502| ``\r``          | ASCII Carriage Return (CR)      |       |
503+-----------------+---------------------------------+-------+
504| ``\t``          | ASCII Horizontal Tab (TAB)      |       |
505+-----------------+---------------------------------+-------+
506| ``\uxxxx``      | Character with 16-bit hex value | \(1)  |
507|                 | *xxxx* (Unicode only)           |       |
508+-----------------+---------------------------------+-------+
509| ``\Uxxxxxxxx``  | Character with 32-bit hex value | \(2)  |
510|                 | *xxxxxxxx* (Unicode only)       |       |
511+-----------------+---------------------------------+-------+
512| ``\v``          | ASCII Vertical Tab (VT)         |       |
513+-----------------+---------------------------------+-------+
514| ``\ooo``        | Character with octal value      | (3,5) |
515|                 | *ooo*                           |       |
516+-----------------+---------------------------------+-------+
517| ``\xhh``        | Character with hex value *hh*   | (4,5) |
518+-----------------+---------------------------------+-------+
519
520.. index:: single: ASCII@ASCII
521
522Notes:
523
524(1)
525   Individual code units which form parts of a surrogate pair can be encoded using
526   this escape sequence.
527
528(2)
529   Any Unicode character can be encoded this way, but characters outside the Basic
530   Multilingual Plane (BMP) will be encoded using a surrogate pair if Python is
531   compiled to use 16-bit code units (the default).
532
533(3)
534   As in Standard C, up to three octal digits are accepted.
535
536(4)
537   Unlike in Standard C, exactly two hex digits are required.
538
539(5)
540   In a string literal, hexadecimal and octal escapes denote the byte with the
541   given value; it is not necessary that the byte encodes a character in the source
542   character set. In a Unicode literal, these escapes denote a Unicode character
543   with the given value.
544
545.. index:: single: unrecognized escape sequence
546
547Unlike Standard C, all unrecognized escape sequences are left in the string
548unchanged, i.e., *the backslash is left in the string*.  (This behavior is
549useful when debugging: if an escape sequence is mistyped, the resulting output
550is more easily recognized as broken.)  It is also important to note that the
551escape sequences marked as "(Unicode only)" in the table above fall into the
552category of unrecognized escapes for non-Unicode string literals.
553
554When an ``'r'`` or ``'R'`` prefix is present, a character following a backslash
555is included in the string without change, and *all backslashes are left in the
556string*.  For example, the string literal ``r"\n"`` consists of two characters:
557a backslash and a lowercase ``'n'``.  String quotes can be escaped with a
558backslash, but the backslash remains in the string; for example, ``r"\""`` is a
559valid string literal consisting of two characters: a backslash and a double
560quote; ``r"\"`` is not a valid string literal (even a raw string cannot end in
561an odd number of backslashes).  Specifically, *a raw string cannot end in a
562single backslash* (since the backslash would escape the following quote
563character).  Note also that a single backslash followed by a newline is
564interpreted as those two characters as part of the string, *not* as a line
565continuation.
566
567When an ``'r'`` or ``'R'`` prefix is used in conjunction with a ``'u'`` or
568``'U'`` prefix, then the ``\uXXXX`` and ``\UXXXXXXXX`` escape sequences are
569processed while  *all other backslashes are left in the string*. For example,
570the string literal ``ur"\u0062\n"`` consists of three Unicode characters: 'LATIN
571SMALL LETTER B', 'REVERSE SOLIDUS', and 'LATIN SMALL LETTER N'. Backslashes can
572be escaped with a preceding backslash; however, both remain in the string.  As a
573result, ``\uXXXX`` escape sequences are only recognized when there are an odd
574number of backslashes.
575
576
577.. _string-catenation:
578
579String literal concatenation
580----------------------------
581
582Multiple adjacent string literals (delimited by whitespace), possibly using
583different quoting conventions, are allowed, and their meaning is the same as
584their concatenation.  Thus, ``"hello" 'world'`` is equivalent to
585``"helloworld"``.  This feature can be used to reduce the number of backslashes
586needed, to split long strings conveniently across long lines, or even to add
587comments to parts of strings, for example::
588
589   re.compile("[A-Za-z_]"       # letter or underscore
590              "[A-Za-z0-9_]*"   # letter, digit or underscore
591             )
592
593Note that this feature is defined at the syntactical level, but implemented at
594compile time.  The '+' operator must be used to concatenate string expressions
595at run time.  Also note that literal concatenation can use different quoting
596styles for each component (even mixing raw strings and triple quoted strings).
597
598
599.. _numbers:
600
601Numeric literals
602----------------
603
604.. index::
605   single: number
606   single: numeric literal
607   single: integer literal
608   single: plain integer literal
609   single: long integer literal
610   single: floating point literal
611   single: hexadecimal literal
612   single: binary literal
613   single: octal literal
614   single: decimal literal
615   single: imaginary literal
616   single: complex; literal
617
618There are four types of numeric literals: plain integers, long integers,
619floating point numbers, and imaginary numbers.  There are no complex literals
620(complex numbers can be formed by adding a real number and an imaginary number).
621
622Note that numeric literals do not include a sign; a phrase like ``-1`` is
623actually an expression composed of the unary operator '``-``' and the literal
624``1``.
625
626
627.. _integers:
628
629Integer and long integer literals
630---------------------------------
631
632Integer and long integer literals are described by the following lexical
633definitions:
634
635.. productionlist::
636   longinteger: `integer` ("l" | "L")
637   integer: `decimalinteger` | `octinteger` | `hexinteger` | `bininteger`
638   decimalinteger: `nonzerodigit` `digit`* | "0"
639   octinteger: "0" ("o" | "O") `octdigit`+ | "0" `octdigit`+
640   hexinteger: "0" ("x" | "X") `hexdigit`+
641   bininteger: "0" ("b" | "B") `bindigit`+
642   nonzerodigit: "1"..."9"
643   octdigit: "0"..."7"
644   bindigit: "0" | "1"
645   hexdigit: `digit` | "a"..."f" | "A"..."F"
646
647Although both lower case ``'l'`` and upper case ``'L'`` are allowed as suffix
648for long integers, it is strongly recommended to always use ``'L'``, since the
649letter ``'l'`` looks too much like the digit ``'1'``.
650
651Plain integer literals that are above the largest representable plain integer
652(e.g., 2147483647 when using 32-bit arithmetic) are accepted as if they were
653long integers instead. [#]_  There is no limit for long integer literals apart
654from what can be stored in available memory.
655
656Some examples of plain integer literals (first row) and long integer literals
657(second and third rows)::
658
659   7     2147483647                        0177
660   3L    79228162514264337593543950336L    0377L   0x100000000L
661         79228162514264337593543950336             0xdeadbeef
662
663
664.. _floating:
665
666Floating point literals
667-----------------------
668
669Floating point literals are described by the following lexical definitions:
670
671.. productionlist::
672   floatnumber: `pointfloat` | `exponentfloat`
673   pointfloat: [`intpart`] `fraction` | `intpart` "."
674   exponentfloat: (`intpart` | `pointfloat`) `exponent`
675   intpart: `digit`+
676   fraction: "." `digit`+
677   exponent: ("e" | "E") ["+" | "-"] `digit`+
678
679Note that the integer and exponent parts of floating point numbers can look like
680octal integers, but are interpreted using radix 10.  For example, ``077e010`` is
681legal, and denotes the same number as ``77e10``. The allowed range of floating
682point literals is implementation-dependent. Some examples of floating point
683literals::
684
685   3.14    10.    .001    1e100    3.14e-10    0e0
686
687Note that numeric literals do not include a sign; a phrase like ``-1`` is
688actually an expression composed of the unary operator ``-`` and the literal
689``1``.
690
691
692.. _imaginary:
693
694Imaginary literals
695------------------
696
697Imaginary literals are described by the following lexical definitions:
698
699.. productionlist::
700   imagnumber: (`floatnumber` | `intpart`) ("j" | "J")
701
702An imaginary literal yields a complex number with a real part of 0.0.  Complex
703numbers are represented as a pair of floating point numbers and have the same
704restrictions on their range.  To create a complex number with a nonzero real
705part, add a floating point number to it, e.g., ``(3+4j)``.  Some examples of
706imaginary literals::
707
708   3.14j   10.j    10j     .001j   1e100j  3.14e-10j
709
710
711.. _operators:
712
713Operators
714=========
715
716.. index:: single: operators
717
718The following tokens are operators:
719
720.. code-block:: none
721
722
723   +       -       *       **      /       //      %
724   <<      >>      &       |       ^       ~
725   <       >       <=      >=      ==      !=      <>
726
727The comparison operators ``<>`` and ``!=`` are alternate spellings of the same
728operator.  ``!=`` is the preferred spelling; ``<>`` is obsolescent.
729
730
731.. _delimiters:
732
733Delimiters
734==========
735
736.. index:: single: delimiters
737
738The following tokens serve as delimiters in the grammar:
739
740.. code-block:: none
741
742   (       )       [       ]       {       }      @
743   ,       :       .       `       =       ;
744   +=      -=      *=      /=      //=     %=
745   &=      |=      ^=      >>=     <<=     **=
746
747The period can also occur in floating-point and imaginary literals.  A sequence
748of three periods has a special meaning as an ellipsis in slices. The second half
749of the list, the augmented assignment operators, serve lexically as delimiters,
750but also perform an operation.
751
752The following printing ASCII characters have special meaning as part of other
753tokens or are otherwise significant to the lexical analyzer:
754
755.. code-block:: none
756
757   '       "       #       \
758
759.. index:: single: ASCII@ASCII
760
761The following printing ASCII characters are not used in Python.  Their
762occurrence outside string literals and comments is an unconditional error:
763
764.. code-block:: none
765
766   $       ?
767
768.. rubric:: Footnotes
769
770.. [#] In versions of Python prior to 2.4, octal and hexadecimal literals in the range
771   just above the largest representable plain integer but below the largest
772   unsigned 32-bit number (on a machine using 32-bit arithmetic), 4294967296, were
773   taken as the negative plain integer obtained by subtracting 4294967296 from
774   their unsigned value.
775
776