Python 内建函数 - complex([real[, imag]])
- Manual
- 直譯
- 實(shí)例
- 拓展閱讀
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.
直譯
以實(shí)部+虛部*1j形式返回一個(gè)復(fù)數(shù),或?qū)⒆址驍?shù)字轉(zhuǎn)化為復(fù)數(shù)。如果第一個(gè)參數(shù)是字符串,它將會被轉(zhuǎn)化為復(fù)數(shù),且函數(shù)的調(diào)用不能有第二個(gè)參數(shù),第二個(gè)參數(shù)永遠(yuǎn)不能是字符串。每個(gè)參數(shù)可以使任意數(shù)值類型(包括復(fù)數(shù))。如果缺省虛部,它默認(rèn)為0,構(gòu)造器會以整數(shù)或浮點(diǎn)數(shù)值進(jìn)行對待。如果兩個(gè)參數(shù)都缺省,則返回0j。
注意:當(dāng)從一個(gè)字符串轉(zhuǎn)化時(shí),字符串在中間的‘+’或‘-’操作符周圍不能包含空格。例如:復(fù)數(shù)complex('1+2j')可行,但complex('1 + 2j')則會引發(fā)ValueError。復(fù)數(shù)類型詳見數(shù)值類型—整數(shù),浮點(diǎn)數(shù)和復(fù)數(shù)
實(shí)例
>>> 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拓展閱讀
數(shù)值類型—整數(shù),浮點(diǎn)數(shù)和復(fù)數(shù)
總結(jié)
以上是生活随笔為你收集整理的Python 内建函数 - complex([real[, imag]])的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab实现RGB图转为BAYER图
- 下一篇: 基于JAVA高校多媒体设备运维管理系统服