class complex([real[, imag]])
The complex function returns an imaginary number from the values given as arguments for the real and imaginary part of it, or from a text string containing the representation of a complex number.
Either argument can be of any numeric type, including complex numbers (see example below). If the second argument is omitted, a value of 0 is assumed. If the two arguments are omitted, both values are assumed to be. The first argument cannot be omitted if the second argument is to be added.
- real: real part of the complex number to represent or text string containing the representation of a complex number.
- imag: (optional) imaginary part of the complex number to represent. It cannot be a text string.
Examples are shown below in which the two arguments are added (both being integers), adding only real part or only imaginary part (and indicating the value 0 for the real part):
If some of the arguments is a complex number, the function adds the real and imaginary parts:
The first argument can be a text string representing a complex number. In this case, this argument cannot contain spaces around the + or - sign that joins the real and imaginary parts, and the function cannot include the second argument:
If both arguments are omitted, the complex number 0 + 0j is returned: