Pythoncomplex()

更新时间:2023-05-16 04:22:21 阅读: 评论:0

私营企业
Pythoncomplex()
Python complex() function is ud to create complex numbers. It’s a built-in function that returns a complex from the input parameters.
Python complex()函数⽤于创建复数。 这是⼀个内置函数,可从输⼊参数返回复杂 。
Python complex() (Python complex())
Python complex() function syntax is:
Python complex()函数语法为:
class complex([real[, imag]])
Some of the important points to remember while using complex() function are:
使⽤complex()函数时要记住的⼀些重要要点是:
Python complex() function returns complex number with value real + imag*1j.
Python complex()函数返回值为real + imag*1j复数。
We can convert to complex number using complex() function. If the first argument is string, cond argument is not allowed.
塑身背心
我们可以使⽤complex()函数将转换为复数。 如果第⼀个参数是字符串,则不允许第⼆个参数。
While converting string to complex number, whitespaces in string is not allowed. For example, ‘1+2j’ is fine but ‘1 + 2j’ is not.
将字符串转换为复数时,不允许在字符串中使⽤空格。 例如,“ 1 + 2j”很好,但“ 1 + 2j”不是。
If no argument is pasd, then 0j is returned.
如果未传递任何参数,则返回0j 。
Input arguments can be in any numeric format such as hexadecimal, binary etc.
输⼊参数可以是任何数字格式,例如⼗六进制,⼆进制等。
Underscores in are also allowed from Python 3.6 onwards, refer .
从Python 3.6开始,也允许在中使⽤下划线,请参阅 。
纸飞镖
Python complex()⽰例 (Python complex() examples)
Let’s look at some complex() function examples.
让我们看⼀些complex()函数⽰例。
没有任何参数的complex() (complex() without any argument)
c = complex()
print(type(c))
print(c)
Output:
输出:
<class 'complex'>
0j
带数字参数的complex() (complex() with numeric arguments)
c = complex(1, 1)
print(c)
c = complex(1.5, -2.1)
print(c)
c = complex(0xF)  # hexadecimal
碎纸机卡纸print(c)
c = complex(0b1010, -1)  # binary
print(c)
Output:
输出:
(1+1j)
(1.5-2.1j)
(15+0j)
(10-1j)
带下划线数字⽂字的complex() (complex() with underscore numeric literals)
c = complex(10_000_000.0, -2)
print(c)营养师就业
Output: (10000000-2j)
输出: (10000000-2j)
爱乌及屋带复数输⼊的complex() (complex() with complex number inputs)
c = 1 + 2j
c = complex(c, -4)
print(c)
c = complex(1+2j, 1+2J)
print(c)
Output:
输出:
(1-2j)
(-1+3j)
Notice how complex literals are combined to form a new complex number. If the cond argument is a complex number, then the calculation is different.
请注意,复杂字⾯量是如何组合形成⼀个新的复数的。 如果第⼆个参数是复数,则计算将不同。
带字符串参数的complex() (complex() with string arguments)
c = complex("1+2j")
print(c)
Output: (1+2j)
输出: (1+2j)
Let’s e what happens when the input string has white spaces.
让我们看看当输⼊字符串带有空格时会发⽣什么。
c = complex("1 + 2j")
Output: ValueError: complex() arg is a malformed string
输出: ValueError: complex() arg is a malformed string
Let’s e what happens when the first argument is a string and the cond argument is also provided.让我们看看当第⼀个参数是⼀个字符串并且还提供了第⼆个参数时会发⽣什么。
c = complex("1+j", 2)
Output: TypeError: complex() can't take cond arg if first is a string
输出: TypeError: complex() can't take cond arg if first is a string
What if we try to pass the cond argument as String, let’s e what error we get.
如果我们尝试将第⼆个参数作为String传递,该怎么办,让我们看看发⽣了什么错误。
c = complex(2, "-2j")
心服口服的意思
Output: TypeError: complex() cond arg can't be a string
输出: TypeError: complex() cond arg can't be a string
That’s all for python complex() function.
这就是python complex()函数的全部内容。
学习方式
. 检出完整的python脚本和更多Python⽰例。
Reference:
参考:

本文发布于:2023-05-16 04:22:21,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/649516.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:参数   字符串   复数   函数   允许   空格
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图