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日志模块logging简介
Apr 13 Python
Python标准库defaultdict模块使用示例
Apr 28 Python
Python实现telnet服务器的方法
Jul 10 Python
Python中return语句用法实例分析
Aug 04 Python
Python编程实现及时获取新邮件的方法示例
Aug 10 Python
tensorflow实现简单的卷积网络
May 24 Python
Python代码块批量添加Tab缩进的方法
Jun 25 Python
python实战教程之自动扫雷
Jul 13 Python
使用Python实现跳帧截取视频帧
May 31 Python
如何关掉pycharm中的python console(图解)
Oct 31 Python
Python GUI库PyQt5样式QSS子控件介绍
Feb 25 Python
python实现发送带附件的邮件代码分享
Sep 22 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
图片存储与浏览一例(Linux+Apache+PHP+MySQL)
2006/10/09 PHP
色色整理的PHP面试题集锦
2012/03/08 PHP
深入探讨:Nginx 502 Bad Gateway错误的解决方法
2013/06/03 PHP
php从csv文件读取数据并输出到网页的方法
2015/03/14 PHP
WordPress中登陆后关闭登陆页面及设置用户不可见栏目
2015/12/31 PHP
PHP抓取远程图片(含不带后缀的)教程详解
2016/10/21 PHP
TP(thinkPHP)框架多层控制器和多级控制器的使用示例
2018/06/13 PHP
制作特殊字的脚本
2006/06/26 Javascript
document.all与WEB标准
2020/05/13 Javascript
IE不出现Flash激活框的小发现的js实现方法
2007/09/07 Javascript
JavaScript Event学习第六章 事件的访问
2010/02/07 Javascript
javascript中递归函数用法注意点
2015/07/30 Javascript
Angular2实现自定义双向绑定属性
2017/03/22 Javascript
javascript简单写的判断电话号码实例
2017/05/24 Javascript
Vue使用NPM方式搭建项目
2018/10/25 Javascript
vue中使用better-scroll实现滑动效果及注意事项
2018/11/15 Javascript
layui加载表格,绑定新增,编辑删除,查看按钮事件的例子
2019/09/06 Javascript
JS访问对象两种方式区别解析
2020/08/29 Javascript
JavaScript代码实现简单计算器
2020/12/27 Javascript
[02:48]DOTA2英雄基础教程 拉席克
2013/12/12 DOTA
django开发教程之利用缓存文件进行页面缓存的方法
2017/11/10 Python
目前最全的python的就业方向
2018/06/05 Python
Python面向对象类编写细节分析【类,方法,继承,超类,接口等】
2019/01/05 Python
numpy创建单位矩阵和对角矩阵的实例
2019/11/29 Python
Python 过滤错误log并导出的实例
2019/12/26 Python
Python注释、分支结构、循环结构、伪“选择结构”用法实例分析
2020/01/09 Python
python实现音乐播放器 python实现花框音乐盒子
2020/02/25 Python
基于pycharm实现批量修改变量名
2020/06/02 Python
浅谈多卡服务器下隐藏部分 GPU 和 TensorFlow 的显存使用设置
2020/06/30 Python
python GUI计算器的实现
2020/10/09 Python
英国床和浴室商场:Bed & Bath Emporium
2018/05/20 全球购物
国际贸易专业个人求职信格式
2014/02/02 职场文书
2015年幼儿园毕业感言
2014/02/12 职场文书
介绍信范文大全
2015/05/07 职场文书
优质护理心得体会
2016/01/22 职场文书
一篇文章了解正则表达式的替换技巧
2022/02/24 Javascript