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 条件判断的缩写方法
Sep 06 Python
Python列表和元组的定义与使用操作示例
Jul 26 Python
python批量替换多文件字符串问题详解
Apr 22 Python
windows下安装Python的XlsxWriter模块方法
May 03 Python
python模拟登陆,用session维持回话的实例
Dec 27 Python
Django之路由层的实现
Sep 09 Python
python tkinter之顶层菜单、弹出菜单实例
Mar 04 Python
python实现梯度下降法
Mar 24 Python
django rest framework serializer返回时间自动格式化方法
Mar 31 Python
Django调用支付宝接口代码实例详解
Apr 04 Python
通过实例简单了解python yield使用方法
Aug 06 Python
Python编写可视化界面的全过程(Python+PyCharm+PyQt)
May 17 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
PHP5.0正式发布 不完全兼容PHP4 新增多项功能
2006/10/09 PHP
坏狼php学习 计数器实例代码
2008/06/15 PHP
PHP 时间日期操作实战
2011/08/26 PHP
php给每个段落添加空格的方法
2015/03/20 PHP
通过修改referer下载文件的方法
2008/05/11 Javascript
javascript html 静态页面传参数
2009/04/10 Javascript
jquery隔行换色效果实现方法
2015/01/15 Javascript
使用Sticker.js实现贴纸效果
2015/01/28 Javascript
由ReactJS的Hello world说开来
2015/07/02 Javascript
JavaScript识别网页关键字并进行描红的方法
2015/11/09 Javascript
jqGrid中文文档之选项设置
2015/12/02 Javascript
学习jQuey中的return false
2015/12/18 Javascript
深入理解Angular2 模板语法
2016/08/07 Javascript
浅谈移动端之js touch事件 手势滑动事件
2016/11/07 Javascript
Nodejs高扩展性的模板引擎 functmpl简介
2017/02/13 NodeJs
AngulaJS路由 ui-router 传参实例
2017/04/28 Javascript
JavaScript运动框架 多值运动(四)
2017/05/18 Javascript
Vue非父子组件通信详解
2017/06/12 Javascript
老生常谈Bootstrap媒体对象
2017/07/06 Javascript
jQuery选择器选中最后一个元素,倒数第二个元素操作示例
2018/12/10 jQuery
使用VueRouter的addRoutes方法实现动态添加用户的权限路由
2019/06/03 Javascript
使用axios发送post请求,将JSON数据改为form类型的示例
2019/10/31 Javascript
微信小程序跨页面数据传递事件响应实现过程解析
2019/12/19 Javascript
ES6字符串的扩展实例
2020/12/21 Javascript
python中函数默认值使用注意点详解
2016/06/01 Python
python+opencv实现的简单人脸识别代码示例
2017/11/14 Python
python简单商城购物车实例代码
2018/03/15 Python
python通过txt文件批量安装依赖包的实现步骤
2019/08/13 Python
Python 爬虫批量爬取网页图片保存到本地的实现代码
2020/12/24 Python
HTML5+CSS3 实现灵动的动画 TAB 切换效果(DEMO)
2017/09/15 HTML / CSS
司机岗位职责
2013/11/15 职场文书
车贷收入证明范本
2014/01/09 职场文书
学生安全承诺书
2014/05/22 职场文书
学雷锋志愿者活动总结
2014/06/27 职场文书
CSS3 制作精美的定价表
2021/04/06 HTML / CSS
python3实现Dijkstra算法最短路径的实现
2021/05/12 Python