The complex() function in Python is a built-in function that returns a complex number by specifying a real part and an optional imaginary part. It can be called with either two arguments (real and imag) or a single string argument that represents the complex number.
Parameter Values
| Parameter | Description |
|---|---|
| real | The real part of the complex number. Can be an integer, float, or string that can be converted to a floating-point number. |
| imag | The imaginary part of the complex number. Can be an integer, float, or string that can be converted to a floating-point number. |
Return Values
The complex() function returns a complex number or ValueError on invalid input.
How to Use complex() in Python
The complex() function returns a complex number by converting the arguments to complex numbers.
complex(3, 4)If the first argument is a string, it should also provide the second argument (the imaginary part of the complex number).
complex('5+2j')