complex([real[, imag]])
中文說明:
建立一個值為real + imag * j的複數或者轉化一個字串或數為複數。如果第一個引數為字串,則不需要指定第二個引數。
引數real: int, long, float或字串;
引數imag: int, long, float。
英文說明:
Create a complex number with the value real + imag*j 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 function serves as a numeric conversion function like int(), long() 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.
The complex type is described in Numeric Types — int, float, long, complex.
程式碼例項:
>>> complex(1, 2) (1 + 2j) #數字 >>> complex(1) (1 + 0j) #當做字串處理 >>> complex("1") (1 + 0j) #注意:這個地方在“+”號兩邊不能有空格,也就是不能寫成"1 + 2j",應該是"1+2j",否則會報錯 >>> complex("1+2j") (1 + 2j)