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之日期与时间处理模块(date和datetime)
Feb 16 Python
Pycharm学习教程(3) 代码运行调试
May 03 Python
python数据封装json格式数据
Mar 04 Python
Python日志模块logging基本用法分析
Aug 23 Python
Django csrf 两种方法设置form的实例
Feb 03 Python
Python元组知识点总结
Feb 18 Python
如何不用安装python就能在.NET里调用Python库
Jul 12 Python
python之列表推导式的用法
Nov 29 Python
django框架中间件原理与用法详解
Dec 10 Python
pycharm实现在虚拟环境中引入别人的项目
Mar 09 Python
教你怎么用Python生成九宫格照片
May 20 Python
粗暴解决CUDA out of memory的问题
May 22 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
header()函数使用说明
2006/11/23 PHP
PHP Directory 函数的详解
2013/03/07 PHP
php页面跳转代码 输入网址跳转到你定义的页面
2013/03/28 PHP
php中base64_decode与base64_encode加密解密函数实例
2014/11/24 PHP
PHP聚合式迭代器接口IteratorAggregate用法分析
2017/12/28 PHP
TP5框架使用QueryList采集框架爬小说操作示例
2020/03/26 PHP
javascript中的location用法简单介绍
2007/03/07 Javascript
JQuery小知识
2010/10/15 Javascript
jQuery中is()方法用法实例
2015/01/06 Javascript
简单介绍JavaScript的变量和数据类型
2015/06/03 Javascript
js实现图片点击左右轮播
2015/07/08 Javascript
JavaScript的类型、值和变量小结
2015/07/09 Javascript
indexedDB bootstrap angularjs之 MVC DOMO (应用示例)
2016/06/20 Javascript
js仿腾讯QQ的web登陆界面
2016/08/19 Javascript
用jquery快速解决IE输入框不能输入的问题
2016/10/04 Javascript
jQuery Validate 相关参数及常用的自定义验证规则
2017/03/06 Javascript
jQuery+HTML5实现WebGL高性能烟花绽放动画效果【附demo源码下载】
2017/08/18 jQuery
javaScript产生随机数的用法小结
2018/04/21 Javascript
JS实现十分钟倒计时代码实例
2018/10/18 Javascript
es6中Promise 对象基本功能与用法实例分析
2020/02/23 Javascript
Vue项目接入Paypal实现示例详解
2020/06/04 Javascript
[01:01:24]LGD vs Fnatic 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
Numpy array数据的增、删、改、查实例
2018/06/04 Python
浅谈Python type的使用
2019/11/19 Python
python 消费 kafka 数据教程
2019/12/21 Python
Django认证系统user对象实现过程解析
2020/03/02 Python
Python 调用有道翻译接口实现翻译
2020/03/02 Python
Python3+Flask安装使用教程详解
2021/02/16 Python
Ted Baker美国官网:英国时尚品牌
2018/10/29 全球购物
实现向右循环移位
2014/07/31 面试题
白酒市场营销方案
2014/02/25 职场文书
亲子活动总结
2014/04/26 职场文书
师范生求职自荐信
2014/06/14 职场文书
演讲开场白和结束语
2015/05/29 职场文书
话题作文之诚信
2019/11/28 职场文书
python 中的jieba分词库
2021/11/23 Python