|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 一个账号 于 2020-3-14 21:14 编辑
Python complex() 函数
语法
参数
参数 | 描述 | real | 复数实数部分,可以是整型、浮点型或者数字字符串,为数字字符串时,不能加 imag 参数 | imag | 复数虚数部分,可以是整型、浮点型,不能是数字字符串 |
描述
complex() 函数通过指定实数和虚数来返回复数。
返回值
返回一个复数。
例子
- >>> complex(3)
- (3+0j)
- >>> complex("123")
- (123+0j)
- >>> complex(4, 2)
- (4+2j)
- >>> complex("5+2j")
- (5+2j)
- >>> complex()
- 0j
- >>> type(complex())
- <class 'complex'>
- >>> complex("5 + 2j") # 不能有空格
- Traceback (most recent call last):
- File "<pyshell#5>", line 1, in <module>
- complex("5 + 2j") # 不能有空格
- ValueError: complex() arg is a malformed string
- >>> complex("4", 2) # 不能有 imag 参数
- Traceback (most recent call last):
- File "<pyshell#11>", line 1, in <module>
- complex("4", 2) # 不能有 imag 参数
- TypeError: complex() can't take second arg if first is a string
- >>> complex(imag="34") # imag 参数不能是数字字符串
- Traceback (most recent call last):
- File "<pyshell#13>", line 1, in <module>
- complex(imag="34") # imag 参数不能是数字字符串
- TypeError: complex() second arg can't be a string
复制代码 |
|