complex(real=0, imag=0)
complex(string)
Returns a complex number with the value real + imag * 1j or converts a string to a complex number if the first argument is a string. The second argument is optional and defaults to 0. If both arguments are omitted, it returns 0j.
Parameters
complex() accepts two parameters:
real- the real part. If omitted, it defaults to 0.imag- the imaginary part. If not specified, it defaults to 0.
If the first parameter is a string, it will be interpreted as a complex number. In this case, do not provide the second parameter.
Return Value
- The
complex()function returns a complex number. - If the string passed to this function is not a valid complex number, a
ValueErroris raised.
Examples
complex(1) # (1+0j)
complex(1, 2) # (1+2j)
complex("1.5+1j") # (1.5+1j)
complex("1.5+2j") # (1.5+2j)