- complex() function take two argument normally which is real and imaginary number.
-But there are also possible to pass a string as argument in complex() function then what will happen
Syntax-
complex(string)
Example-
complex number is n=complex("3","2")
then it will give an error
but when
complex number is n=complex("3")
then it will give 3+0j
complex number is n=complex("1+2j")
then it will give 1+2j
program-
#pass numbers as argument in complex
n=complex(3,2)
print("Complex number is ",n)
print("real part is ",n.real)
print("Imaginary part is ",n.imag)
#pass string as argument
n=complex("3")
print("Complex number is ",n)
print("real part is ",n.real)
print("Imaginary part is",n.imag)
n=complex("3+4j")
print("Complex number is ",n)
print("real part is ",n.real)
print("Imaginary part is",n.imag)
Complex number is (3+2j)
real part is 3.0
Imaginary part is 2.0
Complex number is (3+0j)
real part is 3.0
Imaginary part is 0.0
Complex number is (3+4j)
real part is 3.0
Imaginary part is 4.0
#pass two string as argument
n=complex("3","2")
print("Complex number is ",n)
n=complex("3","2")
TypeError: complex() can't take second arg if first is a string
#pass two string as argument
n=complex("a")
print("Complex number is ",n)
n=complex("a")
ValueError:complex() arg is a malformed string