当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Python complex()用法及代码示例

Python complex() 函数

complex() 函数是 Python 中的一个库函数,用于从给定的实数或/和虚数(这是可选部分)中获取复数,它只接受实数或读取和虚数和返回一个复数。

用法:

    complex(real [,Imaginary])

参数:

  • real– 实数的值
  • Imaginary– 可选参数,默认值为 0。

返回值: complex– 它返回一个复杂类型的数字,其中包含以下形式的值(real + imaginary*j)

例:

    Input:
    real = 10
    img  = 20
        
    print(complex(real))
    print(complex(real, img))
    
    Output:
    (10+0j)
    (10+20j)

获取复数的Python代码

# python code to demonstrate example 
# of complex() function

real = 10
img  = 20

# complex number  by passing real number
print(complex(real))

# complex number  by passing real & img. number
print(complex(real, img))

# complex number by passing real part as 0
print(complex(0))

# complex number by passing real and img. part as 0, 0
print(complex(0,0))

# with float values
real = 10.23
img  = 20.45

# complex number  by passing real number
print(complex(real))

# complex number  by passing real & img. number
print(complex(real, img))

输出

(10+0j)
(10+20j)
0j
0j
(10.23+0j)
(10.23+20.45j)


相关用法


注:本文由纯净天空筛选整理自 complex() function with example in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。