168

Are complex numbers a supported data-type in Python? If so, how do you use them?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
I159
  • 29,741
  • 31
  • 97
  • 132
  • 1
    As you say you are new to maths, can you write what you you want to do in mathematical notation? – mmmmmm Dec 03 '11 at 20:15
  • 26
    I don't think this should have been closed. I also found it confusing that python used the 'j' imaginary syntax common in engineering over the more intuitive 'i' syntax common in math, statistics, R, etc. The first answer below did a good job introducing that. – Mittenchops May 20 '13 at 16:12
  • 2
    It seems a legitimate docbug on Python that `help(complex)` doesn't show any examples, unlike e.g. ` import decimal; help(decimal)` – smci May 03 '17 at 10:16

3 Answers3

255

In python, you can put ‘j’ or ‘J’ after a number to make it imaginary, so you can write complex literals easily:

>>> 1j
1j
>>> 1J
1j
>>> 1j * 1j
(-1+0j)

The ‘j’ suffix comes from electrical engineering, where the variable ‘i’ is usually used for current. (Reasoning found here.)

The type of a complex number is complex, and you can use the type as a constructor if you prefer:

>>> complex(2,3)
(2+3j)

A complex number has some built-in accessors:

>>> z = 2+3j
>>> z.real
2.0
>>> z.imag
3.0
>>> z.conjugate()
(2-3j)

Several built-in functions support complex numbers:

>>> abs(3 + 4j)
5.0
>>> pow(3 + 4j, 2)
(-7+24j)

The standard module cmath has more functions that handle complex numbers:

>>> import cmath
>>> cmath.sin(2 + 3j)
(9.15449914691143-4.168906959966565j)
Community
  • 1
  • 1
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 14
    'i' is also used by mathematicians, physicists, and nearly all other scientists. If that isn't confusing enough, some use 'i' to represent the "positive" square root of one, whereas 'j' is the "negative" square root of one. Thus i == -j. FYJ... – jvriesem Sep 16 '16 at 04:28
  • 2
    @jvriesem "the positive square root of one" is one. Do you mean they use `i` to represent the positive square root of *negative* one? and `j` to represent the negative square root of *negative* one? – joseville Oct 12 '21 at 03:35
  • The i/j argument for current is a bit weak, j is used for current density. – copper.hat Jul 03 '23 at 21:45
18

The following example for complex numbers should be self explanatory including the error message at the end

>>> x=complex(1,2)
>>> print x
(1+2j)
>>> y=complex(3,4)
>>> print y
(3+4j)
>>> z=x+y
>>> print x
(1+2j)
>>> print z
(4+6j)
>>> z=x*y
>>> print z
(-5+10j)
>>> z=x/y
>>> print z
(0.44+0.08j)
>>> print x.conjugate()
(1-2j)
>>> print x.imag
2.0
>>> print x.real
1.0
>>> print x>y

Traceback (most recent call last):
  File "<pyshell#149>", line 1, in <module>
    print x>y
TypeError: no ordering relation is defined for complex numbers
>>> print x==y
False
>>> 
Abhijit
  • 62,056
  • 18
  • 131
  • 204
0

Yes, complex type is supported in Python.

For numbers, Python 3 supports 3 types int, float and complex types as shown below:

print(type(100), isinstance(100, int))
print(type(100.23), isinstance(100.23, float))
print(type(100 + 2j), isinstance(100 + 2j, complex))

Output:

<class 'int'> True
<class 'float'> True
<class 'complex'> True

For numbers, Python 2 supperts 4 types int, long, float and complex types as shown below:

print(type(100), isinstance(100, int))
print(type(10000000000000000000), isinstance(10000000000000000000, long))
print(type(100.23), isinstance(100.23, float))
print(type(100 + 2j), isinstance(100 + 2j, complex))

Output:

(<type 'int'>, True)
(<type 'long'>, True)
(<type 'float'>, True)
(<type 'complex'>, True)
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129