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抓取京东图书评论数据
Aug 31 Python
深入解析Python编程中JSON模块的使用
Oct 15 Python
Python通过Pygame绘制移动的矩形实例代码
Jan 03 Python
在dataframe两列日期相减并且得到具体的月数实例
Jul 03 Python
Python3中内置类型bytes和str用法及byte和string之间各种编码转换 问题
Sep 27 Python
PyGame贪吃蛇的实现代码示例
Nov 21 Python
wxPython窗体拆分布局基础组件
Nov 19 Python
Python下利用BeautifulSoup解析HTML的实现
Jan 17 Python
scrapy爬虫:scrapy.FormRequest中formdata参数详解
Apr 30 Python
scrapy中如何设置应用cookies的方法(3种)
Sep 22 Python
python使用tkinter实现透明窗体上绘制随机出现的小球(实例代码)
May 17 Python
Python 文字识别
May 11 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存储cookie的示例
2014/03/31 PHP
为PHP安装imagick时出现Cannot locate header file MagickWand.h错误的解决方法
2014/11/03 PHP
老生常谈PHP中的数据结构:DS扩展
2017/07/17 PHP
为数据添加append,remove功能
2006/10/03 Javascript
在chrome中window.onload事件的一些问题
2010/03/01 Javascript
JS 无限级 Select效果实现代码(json格式)
2011/08/30 Javascript
JavaScript中的Math 使用介绍
2014/04/21 Javascript
JavaScript如何获取数组最大值和最小值
2015/11/18 Javascript
一道面试题引发的对javascript类型转换的思考
2017/03/06 Javascript
AngularJS $http模块POST请求实现
2017/04/08 Javascript
JS数组操作之增删改查的简单实现
2017/08/21 Javascript
jQuery实现轮播图源码
2019/10/23 jQuery
vue打开新窗口并实现传参的图文实例
2021/03/04 Vue.js
[05:09]第二届DOTA2亚洲邀请赛决赛日比赛集锦:iG 3:0 OG夺冠
2017/04/05 DOTA
[00:32]2018DOTA2亚洲邀请赛VGJ.T出场
2018/04/03 DOTA
Zabbix实现微信报警功能
2016/10/09 Python
python虚拟环境virtualenv的使用教程
2017/10/20 Python
使用urllib库的urlretrieve()方法下载网络文件到本地的方法
2018/12/19 Python
创建Django项目图文实例详解
2019/06/06 Python
python 机器学习之支持向量机非线性回归SVR模型
2019/06/26 Python
python elasticsearch从创建索引到写入数据的全过程
2019/08/04 Python
python 扩展print打印文件路径和当前时间信息的实例代码
2019/10/11 Python
Python使用GitPython操作Git版本库的方法
2020/02/29 Python
python批量修改xml属性的实现方式
2020/03/05 Python
python百行代码自制电脑端网速悬浮窗的实现
2020/05/12 Python
法国大使拉杆箱官网:DELSEY Paris
2018/03/20 全球购物
计算机网络专业个人的自我评价
2013/10/17 职场文书
求职信范文大全
2014/05/26 职场文书
运动员获奖感言
2014/08/15 职场文书
2014年双拥工作总结
2014/11/21 职场文书
介绍信格式样本
2015/05/05 职场文书
《老人与海鸥》教学反思
2016/02/16 职场文书
MySQL官方导出工具mysqlpump的使用
2021/05/21 MySQL
详解nginx进程锁的实现
2021/06/14 Servers
python利用while求100内的整数和方式
2021/11/07 Python
Django框架中模型的用法
2022/06/10 Python