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 相关文章推荐
在Mac OS上使用mod_wsgi连接Python与Apache服务器
Dec 24 Python
Python基于二分查找实现求整数平方根的方法
May 12 Python
Python 绘图和可视化详细介绍
Feb 11 Python
python爬虫的数据库连接问题【推荐】
Jun 25 Python
Python pandas DataFrame操作的实现代码
Jun 21 Python
Django高级编程之自定义Field实现多语言
Jul 02 Python
解析Python3中的Import
Oct 13 Python
python__name__原理及用法详解
Nov 02 Python
解决Jupyter因卸载重装导致的问题修复
Apr 10 Python
Python应用实现处理excel数据过程解析
Jun 19 Python
浅谈keras 模型用于预测时的注意事项
Jun 27 Python
详解用Python把PDF转为Word方法总结
Apr 27 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 CURL模拟GET及POST函数代码
2010/04/25 PHP
PHP版 汉字转码的实现详解
2013/06/09 PHP
PHP中VC6、VC9、TS、NTS版本的区别与用法详解
2013/10/26 PHP
PHP IE中下载附件问题解决方法
2014/01/07 PHP
PHP文件锁函数flock()详细介绍
2014/11/18 PHP
Laravel 5.0 发布 新版本特性详解
2015/02/10 PHP
Linux下从零开始安装配置Nginx服务器+PHP开发环境
2015/12/21 PHP
XHProf报告字段含义的解析
2016/05/17 PHP
Apache PHP MySql安装配置图文教程
2016/08/27 PHP
Laravel给生产环境添加监听事件(SQL日志监听)
2017/06/19 PHP
PHP 使用位运算实现四则运算的代码
2021/03/09 PHP
JSON 入门指南 想了解json的朋友可以看下
2009/08/26 Javascript
为jQuery添加Webkit的触摸的方法分享
2014/02/02 Javascript
javascript中Number对象的toString()方法分析
2014/12/20 Javascript
简单介绍JavaScript的变量和数据类型
2015/06/03 Javascript
Node.js实现文件上传
2016/07/05 Javascript
js动态生成form 并用ajax方式提交的实现方法
2016/09/09 Javascript
详解vue2.6插槽更新v-slot用法总结
2019/03/09 Javascript
Vue+Django项目部署详解
2019/05/30 Javascript
Vue中遍历数组的新方法实例详解
2019/07/21 Javascript
vue数据更新UI不刷新显示的解决办法
2020/08/06 Javascript
[05:46]DOTA2英雄梦之声_第18期_陈
2014/06/20 DOTA
Python通过DOM和SAX方式解析XML的应用实例分享
2015/11/16 Python
在Python中获取两数相除的商和余数方法
2018/11/10 Python
PyCharm设置每行最大长度限制的方法
2019/01/16 Python
Python中BeautifuSoup库的用法使用详解
2019/11/15 Python
python、PyTorch图像读取与numpy转换实例
2020/01/13 Python
python sorted函数原理解析及练习
2020/02/10 Python
使用Html5、CSS实现文字阴影效果
2018/01/17 HTML / CSS
土耳其国际性时尚购物网站:Modanisa
2018/01/19 全球购物
市场部规章制度
2014/01/24 职场文书
优秀班干部事迹材料
2014/01/26 职场文书
一年级学生期末评语
2014/04/21 职场文书
同学毕业留言寄语
2015/02/27 职场文书
同学联谊会邀请函
2019/06/24 职场文书
原型和原型链 prototype和proto的区别详情
2021/11/02 Javascript