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文件读写并使用mysql批量插入示例分享(python操作mysql)
Feb 17 Python
在 Python 应用中使用 MongoDB的方法
Jan 05 Python
Python+Selenium自动化实现分页(pagination)处理
Mar 31 Python
Python基于matplotlib绘制栈式直方图的方法示例
Aug 09 Python
Django使用paginator插件实现翻页功能的实例
Oct 24 Python
python 文本单词提取和词频统计的实例
Dec 22 Python
Python控制键盘鼠标pynput的详细用法
Jan 28 Python
如何使用python操作vmware
Jul 27 Python
python实现差分隐私Laplace机制详解
Nov 25 Python
Keras实现将两个模型连接到一起
May 23 Python
Python就将所有的英文单词首字母变成大写
Feb 12 Python
python解决OpenCV在读取显示图片的时候闪退的问题
Feb 23 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
基于mysql的论坛(4)
2006/10/09 PHP
php5新改动之短标记启用方法
2008/09/11 PHP
php 搜索框提示(自动完成)实例代码
2012/02/05 PHP
使用cookie实现统计访问者登陆次数
2013/06/08 PHP
Destoon模板制作简明教程
2014/06/20 PHP
PHP实现获取并生成数据库字典的方法
2016/05/04 PHP
PHPExcel实现表格导出功能示例【带有多个工作sheet】
2018/06/13 PHP
Table冻结表头示例代码
2013/08/20 Javascript
javascipt:filter过滤介绍及使用
2014/09/10 Javascript
AngularJS入门教程之学习环境搭建
2014/12/06 Javascript
学习JavaScript设计模式之中介者模式
2016/01/14 Javascript
js 性能优化之算法和流程控制
2017/02/15 Javascript
axios封装,使用拦截器统一处理接口,超详细的教程(推荐)
2019/05/02 Javascript
Vue编写可显示周和月模式的日历 Vue自定义日历内容的显示
2019/06/26 Javascript
vue父子组件通信的高级用法示例
2019/08/29 Javascript
[52:36]VGJ.S vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
python在多玩图片上下载妹子图的实现代码
2013/08/13 Python
Python使用django搭建web开发环境
2017/06/09 Python
磁盘垃圾文件清理器python代码实现
2020/08/24 Python
python合并已经存在的sheet数据到新sheet的方法
2018/12/11 Python
python简单贪吃蛇开发
2019/01/28 Python
python内存动态分配过程详解
2019/07/15 Python
django中SMTP发送邮件配置详解
2019/07/19 Python
Python小程序 控制鼠标循环点击代码实例
2019/10/08 Python
python 判断一组数据是否符合正态分布
2020/09/23 Python
CSS3 优势以及网页设计师如何使用CSS3技术
2009/07/29 HTML / CSS
澳大利亚Rockwear官网:女子瑜伽、健身和运动服
2021/01/26 全球购物
毕业生求职简历中的自我评价
2013/10/18 职场文书
查环查孕证明
2014/01/10 职场文书
初中校园之声广播稿
2014/01/15 职场文书
秋天的图画教学反思
2014/05/01 职场文书
Vue接口封装的完整步骤记录
2021/05/14 Vue.js
关于使用Redisson订阅数问题
2022/01/18 Redis
MySQL限制查询和数据排序介绍
2022/03/25 MySQL
frg-100简单操作(设置)说明
2022/04/05 无线电
手把手带你彻底卸载MySQL数据库
2022/06/14 MySQL