Python built-in function complex, pythoncomplex
English document:
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 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 ). if imag is omitted, it defaults to zero and the constructor serves as a numeric conversion like int and float. if both arguments are omitted, returns 0j.
Note
When converting from a string, the string must not contain whitespace around the central + or-operator. for example, complex ('1 + 2j') is fine, but complex ('1 + 2j') raises ValueError.
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)
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!