Python 内置函数complex详解


Posted in Python onOctober 23, 2016

英文文档:

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 相关文章推荐
python 爬虫出现403禁止访问错误详解
Mar 11 Python
Python引用类型和值类型的区别与使用解析
Oct 17 Python
python执行使用shell命令方法分享
Nov 08 Python
浅谈python中对于json写入txt文件的编码问题
Jun 07 Python
python3 cvs将数据读取为字典的方法
Dec 22 Python
Python实现随机取一个矩阵数组的某几行
Nov 26 Python
IDLE下Python文件编辑和运行操作
Apr 25 Python
Java多线程实现四种方式原理详解
Jun 02 Python
win10下python3.8的PIL库安装过程
Jun 08 Python
PySide2出现“ImportError: DLL load failed: 找不到指定的模块”的问题及解决方法
Jun 10 Python
Python浮点型(float)运算结果不正确的解决方案
Sep 22 Python
pandas DataFrame.shift()函数的具体使用
May 24 Python
Python检测生僻字的实现方法
Oct 23 #Python
python 写入csv乱码问题解决方法
Oct 23 #Python
解决Python中字符串和数字拼接报错的方法
Oct 23 #Python
python 读写txt文件 json文件的实现方法
Oct 22 #Python
Python类属性的延迟计算
Oct 22 #Python
如何在Python函数执行前后增加额外的行为
Oct 20 #Python
如何利用Fabric自动化你的任务
Oct 20 #Python
You might like
php zend解密软件绿色版测试可用
2008/04/14 PHP
php获取字符串中各个字符出现次数的方法
2015/02/23 PHP
javascript实现动态CSS换肤技术的脚本
2007/06/29 Javascript
JS获取地址栏参数的小例子
2013/08/23 Javascript
推荐5 个常用的JavaScript调试技巧
2015/01/08 Javascript
jscript读写二进制文件的方法
2015/04/22 Javascript
Vuejs第十二篇之动态组件全面解析
2016/09/09 Javascript
JavaScript贪吃蛇小组件实例代码
2017/08/20 Javascript
JS实现颜色的10进制转化成rgba格式的方法
2017/09/04 Javascript
iconfont的三种使用方式详解
2018/08/05 Javascript
Javascript中弹窗confirm与prompt的区别
2018/10/26 Javascript
微信小程序登录态和检验注册过没的app.js写法
2019/05/22 Javascript
JS实现可用滑块滑动的缓动图代码
2019/09/01 Javascript
vue本地打开build后生成的dist文件夹index.html问题
2019/09/04 Javascript
Servlet返回的数据js解析2种方法
2019/12/12 Javascript
解决vue页面刷新,数据丢失的问题
2020/11/24 Vue.js
element-ui封装一个Table模板组件的示例
2021/01/04 Javascript
Python常见文件操作的函数示例代码
2011/11/15 Python
python通过smpt发送邮件的方法
2015/04/30 Python
离线安装Pyecharts的步骤以及依赖包流程
2020/04/23 Python
简单了解django索引的相关知识
2019/07/17 Python
Python之Matplotlib文字与注释的使用方法
2020/06/18 Python
python时间time模块处理大全
2020/10/25 Python
8款使用 CSS3 实现超炫的 Loading(加载)的动画效果
2015/03/17 HTML / CSS
canvas进阶之贝塞尔公式推导与物体跟随复杂曲线的轨迹运动
2018/01/10 HTML / CSS
荷兰和比利时时尚鞋店:Van Dalen
2018/04/23 全球购物
微软巴西官方网站:Microsoft Brasil
2019/09/26 全球购物
环境科学专业研究生求职信
2013/10/02 职场文书
家长会主持词
2014/03/26 职场文书
《夹竹桃》教学反思
2014/04/20 职场文书
新闻编辑求职信
2014/07/13 职场文书
务虚会发言材料
2014/12/25 职场文书
逃课检讨书
2015/01/26 职场文书
药品销售内勤岗位职责
2015/04/13 职场文书
SQL SERVER中常用日期函数的具体使用
2021/04/08 SQL Server
Linux7.6二进制安装Mysql8.0.27详细操作步骤
2021/11/27 MySQL