@@ -721,20 +721,24 @@ Integer literals
721721Integer literals are described by the following lexical definitions:
722722
723723.. productionlist ::
724- integer: `decimalinteger ` | `octinteger ` | `hexinteger ` | `bininteger `
725- decimalinteger: `nonzerodigit ` `digit`* | "0"+
724+ integer: `decinteger ` | `bininteger ` | `octinteger ` | `hexinteger `
725+ decinteger: `nonzerodigit ` (["_"] `digit `)* | "0"+ (["_"] "0")*
726+ bininteger: "0" ("b" | "B") (["_"] `bindigit `)+
727+ octinteger: "0" ("o" | "O") (["_"] `octdigit `)+
728+ hexinteger: "0" ("x" | "X") (["_"] `hexdigit `)+
726729 nonzerodigit: "1"..."9"
727730 digit: "0"..."9"
728- octinteger: "0" ("o" | "O") `octdigit`+
729- hexinteger: "0" ("x" | "X") `hexdigit`+
730- bininteger: "0" ("b" | "B") `bindigit`+
731+ bindigit: "0" | "1"
731732 octdigit: "0"..."7"
732733 hexdigit: `digit ` | "a"..."f" | "A"..."F"
733- bindigit: "0" | "1"
734734
735735There is no limit for the length of integer literals apart from what can be
736736stored in available memory.
737737
738+ Underscores are ignored for determining the numeric value of the literal. They
739+ can be used to group digits for enhanced readability. One underscore can occur
740+ between digits, and after base specifiers like ``0x ``.
741+
738742Note that leading zeros in a non-zero decimal number are not allowed. This is
739743for disambiguation with C-style octal literals, which Python used before version
7407443.0.
@@ -743,6 +747,10 @@ Some examples of integer literals::
743747
744748 7 2147483647 0o177 0b100110111
745749 3 79228162514264337593543950336 0o377 0xdeadbeef
750+ 100_000_000_000 0b_1110_0101
751+
752+ .. versionchanged :: 3.6
753+ Underscores are now allowed for grouping purposes in literals.
746754
747755
748756.. _floating :
@@ -754,23 +762,28 @@ Floating point literals are described by the following lexical definitions:
754762
755763.. productionlist ::
756764 floatnumber: `pointfloat ` | `exponentfloat `
757- pointfloat: [`intpart `] `fraction ` | `intpart ` "."
758- exponentfloat: (`intpart ` | `pointfloat `) `exponent `
759- intpart : `digit`+
760- fraction: "." `digit`+
761- exponent: ("e" | "E") ["+" | "-"] `digit`+
765+ pointfloat: [`digitpart `] `fraction ` | `digitpart ` "."
766+ exponentfloat: (`digitpart ` | `pointfloat `) `exponent `
767+ digitpart : `digit ` (["_"] ` digit `)*
768+ fraction: "." `digitpart `
769+ exponent: ("e" | "E") ["+" | "-"] `digitpart `
762770
763771Note that the integer and exponent parts are always interpreted using radix 10.
764772For example, ``077e010 `` is legal, and denotes the same number as ``77e10 ``. The
765- allowed range of floating point literals is implementation-dependent. Some
766- examples of floating point literals::
773+ allowed range of floating point literals is implementation-dependent. As in
774+ integer literals, underscores are supported for digit grouping.
775+
776+ Some examples of floating point literals::
767777
768- 3.14 10. .001 1e100 3.14e-10 0e0
778+ 3.14 10. .001 1e100 3.14e-10 0e0 3.14_15_93
769779
770780Note that numeric literals do not include a sign; a phrase like ``-1 `` is
771781actually an expression composed of the unary operator ``- `` and the literal
772782``1 ``.
773783
784+ .. versionchanged :: 3.6
785+ Underscores are now allowed for grouping purposes in literals.
786+
774787
775788.. _imaginary :
776789
@@ -780,15 +793,15 @@ Imaginary literals
780793Imaginary literals are described by the following lexical definitions:
781794
782795.. productionlist ::
783- imagnumber: (`floatnumber ` | `intpart `) ("j" | "J")
796+ imagnumber: (`floatnumber ` | `digitpart `) ("j" | "J")
784797
785798An imaginary literal yields a complex number with a real part of 0.0. Complex
786799numbers are represented as a pair of floating point numbers and have the same
787800restrictions on their range. To create a complex number with a nonzero real
788801part, add a floating point number to it, e.g., ``(3+4j) ``. Some examples of
789802imaginary literals::
790803
791- 3.14j 10.j 10j .001j 1e100j 3.14e-10j
804+ 3.14j 10.j 10j .001j 1e100j 3.14e-10j 3.14_15_93j
792805
793806
794807.. _operators :
0 commit comments