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的WEB框架Flask中使用多个配置文件的解决方法
Apr 18 Python
Python中的匿名函数使用简介
Apr 27 Python
使用Python内置的模块与函数进行不同进制的数的转换
Mar 12 Python
完美解决python遍历删除字典里值为空的元素报错问题
Sep 11 Python
在 Python 应用中使用 MongoDB的方法
Jan 05 Python
python 获取微信好友列表的方法(微信web)
Feb 21 Python
OpenCV-Python 摄像头实时检测人脸代码实例
Apr 30 Python
python GUI库图形界面开发之PyQt5打开保存对话框QFileDialog详细使用方法与实例
Feb 27 Python
Python matplotlib绘制图形实例(包括点,曲线,注释和箭头)
Apr 17 Python
简单了解Python变量作用域正确使用方法
Jun 12 Python
Python实时监控网站浏览记录实现过程详解
Jul 14 Python
一篇文章带你搞定Ubuntu中打开Pycharm总是卡顿崩溃
Nov 02 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
ThinkPHP函数详解之M方法和R方法
2015/09/10 PHP
PHP使用PDO调用mssql存储过程的方法示例
2017/10/07 PHP
分享一个用Mootools写的鼠标滑过进度条改变进度值的实现代码
2011/12/12 Javascript
用JQUERY增删元素的代码
2012/02/14 Javascript
JS高级调试技巧:捕获和分析 JavaScript Error详解
2014/03/16 Javascript
jQuery中scrollLeft()方法用法实例
2015/01/16 Javascript
jQuery+HTML5实现图片上传前预览效果
2015/08/20 Javascript
node.js 动态执行脚本
2016/06/02 Javascript
webpack本地开发环境无法用IP访问的解决方法
2018/03/20 Javascript
在小程序中使用Echart图表的示例代码
2018/08/02 Javascript
全面解析vue router 基本使用(动态路由,嵌套路由)
2018/09/02 Javascript
详解Vue3.0 前的 TypeScript 最佳入门实践
2019/06/18 Javascript
解决ant design vue 表格a-table二次封装,slots渲染的问题
2020/10/28 Javascript
Python中的进程分支fork和exec详解
2015/04/11 Python
python实现支持目录FTP上传下载文件的方法
2015/06/03 Python
Python常用时间操作总结【取得当前时间、时间函数、应用等】
2017/05/11 Python
详解如何用OpenCV + Python 实现人脸识别
2017/10/20 Python
Django使用HttpResponse返回图片并显示的方法
2018/05/22 Python
Python3 pip3 list 出现 DEPRECATION 警告的解决方法
2019/02/16 Python
python爬虫 爬取58同城上所有城市的租房信息详解
2019/07/30 Python
python使用if语句实现一个猜拳游戏详解
2019/08/27 Python
python列表返回重复数据的下标
2020/02/10 Python
解决django 向mysql中写入中文字符出错的问题
2020/05/18 Python
移动Web—CSS为Retina屏幕替换更高质量的图片
2012/12/24 HTML / CSS
HEMA法国:荷兰原创设计
2019/02/21 全球购物
西班牙香水和化妆品购物网站:Arenal Perfumerías
2019/03/01 全球购物
Java提供了哪些企业应用编程接口
2015/02/13 面试题
财务会计专业毕业生自荐信
2013/10/19 职场文书
单位门卫岗位职责
2013/12/20 职场文书
销售行政专员职责
2014/01/03 职场文书
二手房购房意向书范本
2014/04/01 职场文书
承诺书范本
2015/01/21 职场文书
写给导师的自荐信
2015/03/06 职场文书
2015年大学社团工作总结
2015/04/09 职场文书
复活读书笔记
2015/06/29 职场文书
商业计划书如何写?关键问题有哪些?
2019/07/11 职场文书