@@ -922,14 +922,24 @@ Numeric literals
922922 floating-point literal, hexadecimal literal
923923 octal literal, binary literal, decimal literal, imaginary literal, complex literal
924924
925- There are three types of numeric literals: integers, floating-point numbers, and
926- imaginary numbers. There are no complex literals (complex numbers can be formed
927- by adding a real number and an imaginary number).
925+ :token: `NUMBER ` tokens represent numeric literals, of which there are three
926+ types: integers, floating-point numbers, and imaginary numbers.
927+
928+ .. grammar-snippet ::
929+ :group: python-grammar
930+
931+ NUMBER: integer | floatnumber | imagnumber
932+
928933
929934Note that numeric literals do not include a sign; a phrase like ``-1 `` is
930935actually an expression composed of the unary operator '``- ``' and the literal
931936``1 ``.
932937
938+ Similarly, there are no complex literals; a phrase like ``1+2j `` is an
939+ expression composed by the :ref: `integer literal <integers >` ``1 ``,
940+ the :ref: `operator <operators >` '``+ ``',
941+ and the :ref: `imaginary literal <imaginary >` ``2j ``.
942+
933943
934944.. index ::
935945 single: 0b; integer literal
@@ -942,36 +952,67 @@ actually an expression composed of the unary operator '``-``' and the literal
942952Integer literals
943953----------------
944954
945- Integer literals are described by the following lexical definitions :
955+ Integer literals denote whole numbers. For example: :
946956
947- .. productionlist :: python-grammar
948- integer: `decinteger ` | `bininteger ` | `octinteger ` | `hexinteger `
949- decinteger: `nonzerodigit ` (["_"] `digit `)* | "0"+ (["_"] "0")*
950- bininteger: "0" ("b" | "B") (["_"] `bindigit `)+
951- octinteger: "0" ("o" | "O") (["_"] `octdigit `)+
952- hexinteger: "0" ("x" | "X") (["_"] `hexdigit `)+
953- nonzerodigit: "1"..."9"
954- digit: "0"..."9"
955- bindigit: "0" | "1"
956- octdigit: "0"..."7"
957- hexdigit: `digit ` | "a"..."f" | "A"..."F"
957+ 7
958+ 3
959+ 2147483647
958960
959961There is no limit for the length of integer literals apart from what can be
960- stored in available memory.
962+ stored in available memory::
963+
964+ 7922816251426433759354395033679228162514264337593543950336
965+
966+ Underscores can be used to group digits for enhanced readability,
967+ and are ignored for determining the numeric value of the literal.
968+ For example, the following literals are equivalent:
961969
962- Underscores are ignored for determining the numeric value of the literal. They
963- can be used to group digits for enhanced readability. One underscore can occur
964- between digits, and after base specifiers like `` 0x ``.
970+ 100_000_000_000
971+ 100000000000
972+ 1_00_00_00_00_000
965973
966- Note that leading zeros in a non-zero decimal number are not allowed. This is
967- for disambiguation with C-style octal literals, which Python used before version
968- 3.0.
974+ Underscores can only occur between digits.
975+ For example, ``_123 ``, ``321_ ``, and ``123__321 `` are *not * valid literals.
969976
970- Some examples of integer literals::
977+ Integers can be specified in binary (base 2), octal (base 2), or hexadecimal
978+ (base 16) using the prefixes ``0b ``, ``0o `` and ``0x ``, respectively.
979+ Hexadecimal digits 10 through 15 are represented by letters ``A ``-``F ``,
980+ case-insensitive. For example::
971981
972- 7 2147483647 0o177 0b100110111
973- 3 79228162514264337593543950336 0o377 0xdeadbeef
974- 100_000_000_000 0b_1110_0101
982+ 0b100110111
983+ 0b_1110_0101
984+ 0o177
985+ 0o377
986+ 0xdeadbeef
987+ 0xDead_Beef
988+
989+ Underscores can occur between digits or after the base specifier, but not
990+ within it.
991+
992+ For example, ``0x_1f `` is a valid literal, but ``0_x1f `` is not.
993+
994+ Note that leading zeros in a non-zero decimal number are not allowed.
995+ For example, ``0123 `` is not a valid literal.
996+ This is for disambiguation with C-style octal literals, which Python used
997+ before version 3.0.
998+
999+ Formally, integer literals are described by the following lexical definitions:
1000+
1001+ .. grammar-snippet ::
1002+ :group: python-grammar
1003+
1004+ integer: `decinteger ` | `bininteger ` | `octinteger ` |
1005+ `hexinteger ` | `zerointeger `
1006+ decinteger: `nonzerodigit ` (["_"] `digit `)*
1007+ bininteger: "0" ("b" | "B") (["_"] `bindigit `)+
1008+ octinteger: "0" ("o" | "O") (["_"] `octdigit `)+
1009+ hexinteger: "0" ("x" | "X") (["_"] `hexdigit `)+
1010+ zerointeger: "0"+ (["_"] "0")*
1011+ nonzerodigit: "1"..."9"
1012+ digit: "0"..."9"
1013+ bindigit: "0" | "1"
1014+ octdigit: "0"..."7"
1015+ hexdigit: `digit ` | "a"..."f" | "A"..."F"
9751016
9761017.. versionchanged :: 3.6
9771018 Underscores are now allowed for grouping purposes in literals.
0 commit comments