當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。