Python built-in functions (13) -- complex, pythoncomplex
English document:
Classcomplex([Real[,Imag])
Return a complex number with the valueReal+Imag* 1j or convert a string or number to a complex number. if the first parameter is a string, it will 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 may be any numeric type (including complex ). ifImagIs omitted, it defaults to zero and the constructor serves as a numeric conversion likeintAndfloat. If both arguments are omitted, returns0j.
Note
When converting from a string, the string must not contain whitespace around the central+Or-Operator. For example,complex('1+2j')Is fine,complex('1 + 2j')RaisesValueError.
Note:
1. The function returns a plural number. There are two optional parameters.
2. If neither of the two parameters is provided, return the plural 0j.
>>> complex()0j
3. When the first parameter is a string, the second parameter cannot be provided during the call. In this case, the string parameter must be a string that can represent the plural number, and no space is allowed between the plus sign or minus sign.
>>> Complex ('1 + 2j', 2) # The first parameter is a string and cannot accept the second parameter Traceback (most recent call last ): file "<pyshell #2>", line 1, in <module> complex ('1 + 2j', 2) TypeError: complex () can't take second arg if first is a string >>> complex ('1 + 2j') # There cannot be spaces Traceback (most recent call last ): file "<pyshell #3>", line 1, in <module> complex ('1 + 2j') ValueError: complex () arg is a malformed string
4. When the first parameter is int or float, the second parameter can be blank, indicating that the virtual part is 0. If the second parameter is provided, the second parameter also needs to be int or float.
>>> complex(2)(2+0j)>>> complex(2.1,-3.4)(2.1-3.4j)