Python 内建函数 - complex([real[, imag]])
- Manual
- 直譯
- 實例
- 拓展閱讀
Manual
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.The complex type is described in Numeric Types — int, float, complex.
直譯
以實部+虛部*1j形式返回一個復數,或將字符串或數字轉化為復數。如果第一個參數是字符串,它將會被轉化為復數,且函數的調用不能有第二個參數,第二個參數永遠不能是字符串。每個參數可以使任意數值類型(包括復數)。如果缺省虛部,它默認為0,構造器會以整數或浮點數值進行對待。如果兩個參數都缺省,則返回0j。
注意:當從一個字符串轉化時,字符串在中間的‘+’或‘-’操作符周圍不能包含空格。例如:復數complex('1+2j')可行,但complex('1 + 2j')則會引發ValueError。復數類型詳見數值類型—整數,浮點數和復數
實例
>>> complex(0) 0j >>> complex(3, 5) (3+5j) >>> complex(2.25, 163.33) (2.25+163.33j) >>> complex('1+2j') (1+2j) >>> complex('1 + 2j') Traceback (most recent call last):File "<pyshell#144>", line 1, in <module>complex('1 + 2j') ValueError: complex() arg is a malformed string >>> complex('1+2j', '6j') Traceback (most recent call last):File "<pyshell#145>", line 1, in <module>complex('1+2j', '6j') TypeError: complex() can't take second arg if first is a string拓展閱讀
數值類型—整數,浮點數和復數
總結
以上是生活随笔為你收集整理的Python 内建函数 - complex([real[, imag]])的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab实现RGB图转为BAYER图
- 下一篇: python 获取qq群成员信息_用Py