The Python complex()
is a built-in function that returns a complex number with real and imaginary values (eg. real + imag*j
). It converts a string or a number into a complex number.
complex([real[,imag]])
As you can see in the syntax above, the complex()
function takes two parameters:
0
.0
if omitted.If the first parameter is a string, it will be interpreted as a complex number and the function must be called without a second parameter. The second parameter can never be a string.
>>> complex()
0j
>>> complex(2)
2+0j
>>> complex(2,3)
2+3j
>>> complex('2) #passing string
2+0j
>>> complex('1+2j')
1+2j
>>> complex(1+1j,2+2j)
3+3j