Python 内置函数complex详解

作者:lqh 时间:2022-02-16 03:57:41 

英文文档:

class complex([real[, imag]])

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.

说明:

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

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


>>> complex()
0j

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


>>> complex('1+2j',2) #第一个参数为字符串,不能接受第二个参数
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
 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 "<pyshell#3>", line 1, in <module>
 complex('1 + 2j')
ValueError: complex() arg is a malformed string

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


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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

标签:Python,complex
0
投稿

猜你喜欢

  • web版Photoshop来了

    2008-04-21 13:39:00
  • IE7新支持的CSS属性和属性选择符

    2008-03-16 14:26:00
  • 在任意字符集下正常显示网页的方法一

    2023-10-12 05:20:46
  • ASP实现SQL语句日期格式的加减运算

    2008-11-07 15:13:00
  • XML简易教程之二

    2008-09-05 17:19:00
  • SQL Server可写脚本和编程扩展SSIS包

    2009-01-20 16:29:00
  • asp如何直接调用Excel数据?

    2010-06-28 18:26:00
  • 利用Python复制文件的9种方法总结

    2022-06-25 15:00:27
  • oracle日期分组查询的完整实例

    2023-06-26 10:14:13
  • 分步启动数据库以重命名数据文件

    2009-03-25 12:33:00
  • Oracle动态交叉表生成

    2010-07-27 12:55:00
  • pytorch训练神经网络爆内存的解决方案

    2023-07-06 01:46:24
  • Golang最大递减数算法问题分析

    2023-07-16 13:49:21
  • PyCharm配置KBEngine快速处理代码提示冲突、配置命令问题

    2023-08-12 09:14:28
  • Python学习之用pygal画世界地图实例

    2021-03-22 13:04:47
  • Python二叉树的镜像转换实现方法示例

    2023-09-28 18:36:35
  • python 遍历可迭代对象的实现方法

    2021-03-02 07:13:07
  • MySQL修改配置,区分大小写

    2010-12-03 16:31:00
  • ASP实例:处理多关键词查询实例代码

    2008-11-24 12:56:00
  • 请给PNG8一个机会:对png8的误解

    2009-09-21 10:45:00
  • asp之家 网络编程 m.aspxhome.com