A complex number

>>> a = j 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'j' is not defined
>>> a = 1j 
>>> a.real 
>>> a.imag 

It is said that the letter ‘j’ is used to denote the imaginary number i in python. But it seems that it alone does not stand for i, right?

Correct. In Python, it’s a suffix on a number, not a constant.


This way, you can freely use j as a variable name without worrying about causing problems for other code that uses complex numbers. Also, being able to write 2j requires special syntax support; it can’t be done by making j be a specific value, first off because the tokenizer wouldn’t split it into 2 and j, and secondly because there would be no multiplication operator.

1 Like

Fun bit of trivia:

MATLAB infamously made the opposite decision and allows i an j to be used on their own to denote the imaginary unit:

>> i
ans =
   0.0000 + 1.0000i
>> j
ans =
   0.0000 + 1.0000i

MATLAB also still allows i or j to be used as variables names.

>> i  = 42
i = 

This caused problems in early versions of MATLAB, because the JIT compiler wasn’t clever enough to differentiate between using i or j as variables and using them as imaginary constants, and would consequently disable some otherwise possible optimizations you defined variables named i or j. This is in part why it is so common to see ii or jj used a loop indices instead of i and j.

See also: