python �支��数转�为整数或浮点数

原文: Python3 数字(Number)

   我无所谓

python �支��数转�为整数或浮点数

>>> float(4.5+0j)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    float(4.5+0j)
TypeError: can't convert complex to float

>>> int(4+0j)
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    int(4+0j)
TypeError: can't convert complex to int
>>>
更多解�

  LL_NUAA

针对��有人�到�数�能强转为int或者float的问题:

其实在Python中,�数�供了2个函数,一个函数是real,返回�数的实数部分,�一个函数是imag,返回�数的虚数部分。因为实数跟�数是差异很大的类型,所以�支�强制转�是�以�解的。因为在强制转�过程中,虚数部分到底该怎么转�,是没有定义的,而int和float�有实数部分,虚数部分该如何��?

>>> a = 4.1+0.3j
>>> a
(4.1+0.3j)
>>> a.real
4.1
>>> a.imag
0.3