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脚本设置超时机制系统时间的方法
Feb 21 Python
Python 编码Basic Auth使用方法简单实例
May 25 Python
pip安装Python库时遇到的问题及解决方法
Nov 23 Python
Flask Web开发入门之文件上传(八)
Aug 17 Python
Python+Tensorflow+CNN实现车牌识别的示例代码
Oct 11 Python
Python程序暂停的正常处理方法
Nov 07 Python
python将邻接矩阵输出成图的实现
Nov 21 Python
python logging通过json文件配置的步骤
Apr 27 Python
python 安装移动复制第三方库操作
Jul 13 Python
浅析关于Keras的安装(pycharm)和初步理解
Oct 23 Python
python 使用tkinter+you-get实现视频下载器
Nov 17 Python
python实现MySQL指定表增量同步数据到clickhouse的脚本
Feb 26 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 正则学习实例
2008/07/30 PHP
PHP实现文字写入图片功能
2019/02/18 PHP
IE7提供XMLHttpRequest对象为兼容
2007/03/08 Javascript
jQuery获取样式中颜色值的方法
2015/01/29 Javascript
如何使用jQuery技术开发ios风格的页面导航菜单
2015/07/29 Javascript
JS实现仿Windows7风格的网页右键菜单效果代码
2015/09/11 Javascript
javascript 判断页面访问方式电脑或者移动端
2016/09/19 Javascript
Vuejs实现购物车功能
2017/11/05 Javascript
小程序实现多列选择器
2019/02/15 Javascript
微信小程序与公众号实现数据互通的方法
2019/07/25 Javascript
在vue项目中使用codemirror插件实现代码编辑器功能
2019/08/27 Javascript
Python中获取网页状态码的两个方法
2014/11/03 Python
Python进行数据提取的方法总结
2016/08/22 Python
安装python3的时候就是输入python3死活没有反应的解决方法
2018/01/24 Python
tensorflow saver 保存和恢复指定 tensor的实例讲解
2018/07/26 Python
pyqt实现.ui文件批量转换为对应.py文件脚本
2019/06/19 Python
Python人工智能之路 之PyAudio 实现录音 自动化交互实现问答
2019/08/13 Python
Python Process多进程实现过程
2019/10/22 Python
pygame实现贪吃蛇游戏(上)
2019/10/29 Python
python网络编程之五子棋游戏
2020/05/14 Python
python如何使用腾讯云发送短信
2020/09/17 Python
结合 CSS3 transition transform 实现简单的跑马灯效果的示例
2018/02/07 HTML / CSS
Ralph Lauren拉夫·劳伦美国官网:带有浓郁美国气息的高品味时装品牌
2017/11/01 全球购物
意大利咖啡、浓缩咖啡和浓缩咖啡机:illy caffe
2019/03/20 全球购物
Ariat英国官网:为世界顶级马术运动员制造最优质的鞋类和服装
2020/02/14 全球购物
北京泡泡网网络有限公司.net面试题
2012/07/17 面试题
师范大学毕业自我鉴定
2013/11/21 职场文书
毕业生的求职信范文分享
2013/12/04 职场文书
《水上飞机》教学反思
2014/04/10 职场文书
财务情况说明书范文
2014/05/06 职场文书
火锅店的活动方案
2014/08/15 职场文书
井冈山红色之旅心得体会
2014/10/07 职场文书
抄袭同学作业检讨书1000字
2014/11/20 职场文书
初婚初育证明范本
2014/11/24 职场文书
奥巴马开学演讲观后感
2015/06/12 职场文书
Spring Boot实战解决高并发数据入库之 Redis 缓存+MySQL 批量入库问题
2022/02/12 Redis