首页
学习
活动
专区
工具
TVP
发布

Python3.6内置函数——complex

英文文档

Return a complex number with the valuereal+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). Ifimagis 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.

complex()

1、函数功能,返回一个复数。有两个可选参数。

2、当两个参数都不提供时,返回复数 0j。

>>> complex>>> complex()0j

3、当第一个参数为字符串时,调用时不能提供第二个参数。此时字符串参数,需是一个能表示复数的字符串,而且加号或者减号左右不能出现空格。

>>> complex('1+2j',2) #第一个参数为字符串,不能接受第二个参数Traceback (most recent call last): File "

", line 1, in complex('1+2j',2) #第一个参数为字符串,不能接受第二个参数TypeError: complex() can't take second arg if first is a string>>> complex('1 + 2j') #不能有空格Traceback (most recent call last): File "

", line 1, in complex('1 + 2j') #不能有空格ValueError: complex() arg is a malformed string

4、当第一个参数为int或者float时,第二个参数可为空,表示虚部为0;如果提供第二个参数,第二个参数也需为int或者float。

>>> complex(2)(2+0j)>>> complex(2.1, -1.9)(2.1-1.9j)

小结

希望通过上面的操作能帮助大家。如果你有什么好的意见,建议,或者有不同的看法,我都希望你留言和我们进行交流、讨论。

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180422A1D8B600?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券