鱼C论坛

 找回密码
 立即注册
查看: 2250|回复: 0

[技术交流] Python complex() 函数

[复制链接]
发表于 2020-3-14 21:09:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 一个账号 于 2020-3-14 21:14 编辑

Python complex() 函数


语法

  1. complex(real=0, imag=0)
复制代码


参数

参数描述
real复数实数部分,可以是整型、浮点型或者数字字符串,为数字字符串时,不能加 imag 参数
imag复数虚数部分,可以是整型、浮点型,不能是数字字符串


描述

complex() 函数通过指定实数和虚数来返回复数。

返回值

返回一个复数。

例子

  1. >>> complex(3)
  2. (3+0j)
  3. >>> complex("123")
  4. (123+0j)
  5. >>> complex(4, 2)
  6. (4+2j)
  7. >>> complex("5+2j")
  8. (5+2j)
  9. >>> complex()
  10. 0j
  11. >>> type(complex())
  12. <class 'complex'>
  13. >>> complex("5 + 2j")   # 不能有空格
  14. Traceback (most recent call last):
  15.   File "<pyshell#5>", line 1, in <module>
  16.     complex("5 + 2j")   # 不能有空格
  17. ValueError: complex() arg is a malformed string
  18. >>> complex("4", 2)    # 不能有 imag 参数
  19. Traceback (most recent call last):
  20.   File "<pyshell#11>", line 1, in <module>
  21.     complex("4", 2)    # 不能有 imag 参数
  22. TypeError: complex() can't take second arg if first is a string
  23. >>> complex(imag="34")     # imag 参数不能是数字字符串
  24. Traceback (most recent call last):
  25.   File "<pyshell#13>", line 1, in <module>
  26.     complex(imag="34")     # imag 参数不能是数字字符串
  27. TypeError: complex() second arg can't be a string
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-7 23:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表