English documents:
class complex
([real[, imag]])
Return a complex number with the value real + imag*1j or convert a string or number to a complex number. If The first parameter is a string, it'll be interpreted as a complex number and the function must be called without a Second parameter. The second parameter can never be a string. Each argument is any numeric type (including complex). If imag is omitted, it defaults to zero and the constructor serves as a numeric conversion like int
and float
. If Both arguments is omitted, returns 0j
.
Note
When converting from a string, the string must not contain whitespace around the central +
or -
operator. For example, was complex(‘1+2j‘)
fine, but complex(‘1 + 2j‘)
raises ValueError
.
Description
1. function function, return a complex number. There are two optional parameters.
2. Returns a complex number 0j when none of the two parameters are available.
>>> Complex () 0j
3. When the first argument is a string, the second argument cannot be supplied when called. In this case, the string parameter is a string that can represent a complex number, and the plus or minus sign cannot have spaces around it.
>>> Complex ('1+2j', 2)#The first argument is a string and cannot accept the second argumentTraceback (most recent): File"<pyshell#2>", Line 1,inch<module>Complex ('1+2j', 2) Typeerror:complex () can'T take second ARG if first is a string>>> Complex ('1 + 2j')#cannot have spacesTraceback (most recent): File"<pyshell#3>", Line 1,inch<module>Complex ('1 + 2j') Valueerror:complex () Arg isA malformed string
4. When the first argument is an int or float, the second argument can be empty, indicating that the imaginary part is 0, and if the second argument is supplied, the second argument must be either int or float.
>>> Complex (2) (0j)>>> Complex (2.1,-3.4) (2.1-3.4j)
Python built-in function (--complex)