Teks sumber — English
Terjemahan — Bahasa Indonesia
(Can also be part of :ref:`numeric literals <numbers>`)
number (``0``-``9``)
:ref:`Numeric literal <numbers>`
A Python program is divided into a number of *logical lines*.
Tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight (this is intended to be the same rule as used by Unix). The total number of spaces preceding the first non-blank character then determines the line's indentation. Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.
Before the first line of the file is read, a single zero is pushed on the stack; this will never be popped off again. The numbers pushed on the stack will always be strictly increasing from bottom to top. At the beginning of each logical line, the line's indentation level is compared to the top of the stack. If it is equal, nothing happens. If it is larger, it is pushed on the stack, and one :data:`!INDENT` token is generated. If it is smaller, it *must* be one of the numbers occurring on the stack; all numbers on the stack that are larger are popped off, and for each number popped off a :data:`!DEDENT` token is generated. At the end of the file, a :data:`!DEDENT` token is generated for each number remaining on the stack that is larger than zero.
Besides :data:`~token.NEWLINE`, :data:`~token.INDENT` and :data:`~token.DEDENT`, the following categories of tokens exist: *identifiers* and *keywords* (:data:`~token.NAME`), *literals* (such as :data:`~token.NUMBER` and :data:`~token.STRING`), and other symbols (*operators* and *delimiters*, :data:`~token.OP`). Whitespace characters (other than logical line terminators, discussed earlier) are not tokens, but serve to delimit tokens. Where ambiguity exists, a token comprises the longest possible string that forms a legal token, when read from left to right.
Unicode category ``<Nl>`` - letter numbers
Unicode category ``<Nd>`` - decimal numbers (includes ``0`` to ``9``)
In terms of lexical analysis, Python has :ref:`string, bytes <strings>` and :ref:`numeric <numbers>` literals.
Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, ``r"\""`` is a valid string literal consisting of two characters: a backslash and a double quote; ``r"\"`` is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, *a raw literal cannot end in a single backslash* (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, *not* as a line continuation.
>>> name = 'Galahad'
>>> favorite_color = 'blue'
>>> print(f'{name}:\t{favorite_color}')
Galahad: blue
>>> print(rf"C:\Users\{name}")
C:\Users\Galahad
>>> print(f'''Three shall be the number of the counting
... and the number of the counting shall be three.''')
Three shall be the number of the counting
and the number of the counting shall be three.
>>> number = 14.3
>>> f'{number:20.7f}'
' 14.3000000'
>>> field_size = 20
>>> precision = 7
>>> f'{number:{field_size}.{precision}f}'
' 14.3000000'
>>> number = 3
>>> f'{number:{field_size}}'
' 3'
>>> f'{number:{field_size:05}}'
'00000000000000000003'
:data:`~token.NUMBER` tokens represent numeric literals, of which there are three types: integers, floating-point numbers, and imaginary numbers.
Integer literals denote whole numbers. For example::
Leading zeros in a non-zero decimal number are not allowed. For example, ``0123`` is not a valid literal. This is for disambiguation with C-style octal literals, which Python used before version 3.0.
Floating-point (float) literals, such as ``3.14`` or ``1.5``, denote :ref:`approximations of real numbers <datamodel-float>`.
Unlike in integer literals, leading zeros are allowed. For example, ``077.010`` is legal, and denotes the same number as ``77.01``.
Optionally, the integer and fraction may be followed by an *exponent*: the letter ``e`` or ``E``, followed by an optional sign, ``+`` or ``-``, and a number in the same format as the integer and fraction parts. The ``e`` or ``E`` represents "times ten raised to the power of"::
Python has :ref:`complex number <typesnumeric>` objects, but no complex literals. Instead, *imaginary literals* denote complex numbers with a zero real part.
For example, in math, the complex number 3+4.2\ *i* is written as the real number 3 added to the imaginary number 4.2\ *i*. Python uses a similar syntax, except the imaginary unit is written as ``j`` rather than *i*::
No whitespace is allowed *within* each token. In particular, the ``j`` suffix, may not be separated from the number before it.
The number before the ``j`` has the same syntax as a floating-point literal. Thus, the following are valid imaginary literals::
Unlike in a floating-point literal the decimal point can be omitted if the imaginary number only has an integer part. The number is still evaluated as a floating-point number, not an integer::
number
number