We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

Numeric literals

There are four types of numeric literals: plain integers, long integers, floating point numbers, and imaginary numbers.

There are no complex literals; complex numbers can be formed by adding a real number and an imaginary number.

Note that numeric literals do not include a sign; a phrase like -1 is actually an expression composed of the unary operator - and the literal 1.

Integer literals

Integer and long integer literals are described by the following lexical definitions:

longinteger ::= [ integer][1] ("l" | "L")
integer ::= [decimalinteger][2] | [ octinteger][3] | [hexinteger][4]
decimalinteger ::= [ nonzerodigit][5] [digit][6]\* | "0"
octinteger ::= "0" [ octdigit][7]+
hexinteger ::= "0" ("x" | "X") [hexdigit][8]+
nonzerodigit ::= "1"..."9"
octdigit ::= "0"..."7"
hexdigit ::= [ digit][6] | "a"..."f" | "A"..."F"

Although both lower case “l” and upper case “L” are allowed as suffix for long integers, it is strongly recommended to always use “L”, since the letter “l” looks too much like the digit “1”.

Plain integer literals that are above the largest representable plain integer (e.g., 2147483647 when using 32-bit arithmetic) are accepted as if they were long integers instead. There is no limit for long integer literals apart from what can be stored in available memory.

Some examples of plain integer literals (first row) and long integer literals (second and third rows):

7     2147483647                        0177
3L    79228162514264337593543950336L    0377L   0x100000000L
      79228162514264337593543950336             0xdeadbeef

In versions of Python prior to 2.4, octal and hexadecimal literals in the range just above the largest representable plain integer but below the largest unsigned 32-bit number (on a machine using 32-bit arithmetic), 4294967296, were taken as the negative plain integer obtained by subtracting 4294967296 from their unsigned value.

Floating-point literals

Floating point literals are described by the following lexical definitions:

floatnumber ::= [ pointfloat][1] | [exponentfloat][2]
pointfloat ::= [[intpart][3]] [fraction][4] | [ intpart][3] "."
exponentfloat ::= ([intpart][3] | [ pointfloat][1]) [exponent][5]
intpart ::= [ digit][6]+
fraction ::= "." [ digit][6]+
exponent ::= ("e" | "E") ["+" | "-"] [digit][6]+

Note that the integer and exponent parts of floating point numbers can look like octal integers, but are interpreted using radix 10. For example, “077e010” is legal, and denotes the same number as “77e10”. The allowed range of floating point literals is implementation-dependent. Some examples of floating point literals:

3.14    10.    .001    1e100    3.14e-10    0e0

Note that numeric literals do not include a sign; a phrase like -1 is actually an expression composed of the operator - and the literal 1.

Imaginary literals

Imaginary literals are described by the following lexical definitions:

imagnumber ::= ([floatnumber][1] | [intpart][2]) (“j” | “J”)

An imaginary literal yields a complex number with a real part of 0.0. Complex numbers are represented as a pair of floating point numbers and have the same restrictions on their range. To create a complex number with a nonzero real part, add a floating point number to it, e.g., (3+4j). Some examples of imaginary literals:

3.14j   10.j    10j     .001j   1e100j  3.14e-10j