python中complex函数_Python 内置函数complex详解

Python 内置函数complex详解

发布于 2016-11-29 17:24:27 | 111 次阅读 | 评论: 0 | 来源: 网友投递

Python编程语言Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。

这篇文章主要介绍了Python 内置函数complex详解的相关资料,需要的朋友可以参考下

英文文档:

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 "", line 1, in

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 "", line 1, in

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 内置函数complex详解

Python内置函数dir详解

Python内置函数OCT详解

Python内置函数bin() oct()等实现进制转换

Python内置函数之filter map reduce使用方法讲解

Python内建数据结构详解

python 内置函数filter

Python内置函数—vars的具体使用方法

python 内置字符串处理函数的使用方法

Python help()函数用法详解

Python内置的字符串处理函数整理

Python常用内置函数总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值