Skip to content

Commit e733902

Browse files
committed
Expand section on imaginary literals
1 parent 4916def commit e733902

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

Doc/reference/lexical_analysis.rst

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -939,11 +939,6 @@ Numeric literals do not include a sign; a phrase like ``-1`` is
939939
actually an expression composed of the unary operator '``-``' and the literal
940940
``1``.
941941

942-
Similarly, there are no complex literals; the expression ``1+2j`` is composed
943-
of the :ref:`integer literal <integers>` ``1``,
944-
the :ref:`operator <operators>` '``+``',
945-
and the :ref:`imaginary literal <imaginary>` ``2j``.
946-
947942

948943
.. index::
949944
single: 0b; integer literal
@@ -1091,18 +1086,55 @@ lexical definitions:
10911086
Imaginary literals
10921087
^^^^^^^^^^^^^^^^^^
10931088

1094-
Imaginary literals are described by the following lexical definitions:
1089+
There are no complex literals.
1090+
Instead, :ref:`complex numbers <typesnumeric>` can be written
1091+
as adding the complex number's real part and imaginary part.
10951092

1096-
.. productionlist:: python-grammar
1097-
imagnumber: (`floatnumber` | `digitpart`) ("j" | "J")
1093+
For example, the imaginary number 3.1+4\ *i* can be written as adding the
1094+
real number 3.1 to the imaginary number 4\ *i*.
1095+
In Python, the imaginary unit is written ``j`` rather than *i* to prevent
1096+
confusion with a traditional short name for an index variable::
1097+
1098+
3.1+4j
1099+
1100+
This is an expression composed
1101+
of the :ref:`float literal <floating>` ``3.1``,
1102+
the :ref:`operator <operators>` '``+``',
1103+
and the :ref:`imaginary literal <imaginary>` ``4j``.
1104+
Since these are three separate tokens, whitespace is allowed between them::
1105+
1106+
3.1 + 4j
1107+
1108+
The ``j`` suffix, however, may not be separated from the numeric part
1109+
of the imaginary literal.
1110+
1111+
The numeric part has the same syntax as a floating-point literal.
1112+
Thus, the following are valid imaginary literals::
10981113

1099-
An imaginary literal yields a complex number with a real part of 0.0. Complex
1100-
numbers are represented as a pair of floating-point numbers and have the same
1101-
restrictions on their range. To create a complex number with a nonzero real
1102-
part, add a floating-point number to it, e.g., ``(3+4j)``. Some examples of
1103-
imaginary literals::
1114+
3.14j
1115+
10.j
1116+
.001j
1117+
1e100j
1118+
3.14e-10j
1119+
3.14_15_93j
11041120

1105-
3.14j 10.j 10j .001j 1e100j 3.14e-10j 3.14_15_93j
1121+
If the number only has an integer part, the decimal point can be omitted.
1122+
The number is still evaluated as a floating-point number, not an integer::
1123+
1124+
10j
1125+
0j
1126+
1000000000000000000000000j # equivalent to 1e+24j
1127+
1128+
The ``j`` suffix is case-insensitive::
1129+
1130+
3.14J # equivalent to 3.14j
1131+
1132+
Formally, imaginary literals are described by the following lexical definition:
1133+
1134+
.. grammar-snippet::
1135+
:group: python-grammar
1136+
1137+
imagnumber: (`floatnumber` | `digitpart`) ("j" | "J")
11061138

11071139

11081140
.. _operators:

0 commit comments

Comments
 (0)