Python 內(nèi)置函數(shù)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. 函數(shù)功能,返回一個復(fù)數(shù)。有兩個可選參數(shù)。
2. 當(dāng)兩個參數(shù)都不提供時,返回復(fù)數(shù) 0j。
>>> complex() 0j
3. 當(dāng)?shù)谝粋€參數(shù)為字符串時,調(diào)用時不能提供第二個參數(shù)。此時字符串參數(shù),需是一個能表示復(fù)數(shù)的字符串,而且加號或者減號左右不能出現(xiàn)空格。
>>> complex('1+2j',2) #第一個參數(shù)為字符串,不能接受第二個參數(shù)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
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 "<pyshell#3>", line 1, in <module>
complex('1 + 2j')
ValueError: complex() arg is a malformed string
4. 當(dāng)?shù)谝粋€參數(shù)為int或者float時,第二個參數(shù)可為空,表示虛部為0;如果提供第二個參數(shù),第二個參數(shù)也需為int或者float。
>>> complex(2) (2+0j) >>> complex(2.1,-3.4) (2.1-3.4j)
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Python實現(xiàn)將HTML轉(zhuǎn)成PDF的方法分析
這篇文章主要介紹了Python實現(xiàn)將HTML轉(zhuǎn)成PDF的方法,結(jié)合實例形式分析了Python基于pdfkit模塊實現(xiàn)HTML轉(zhuǎn)換成PDF文件的相關(guān)操作技巧與注意事項,需要的朋友可以參考下2019-05-05
Python Tornado 實現(xiàn)SSE服務(wù)端主動推送方案
SSE是Server-Sent Events 的簡稱,是一種服務(wù)器端到客戶端(瀏覽器)的單項消息推送,本文主要探索兩個方面的實踐一個是客戶端發(fā)送請求,服務(wù)端的返回是分多次進(jìn)行傳輸?shù)?直到傳輸完成,這種情況下請求結(jié)束后,考慮關(guān)閉SSE,所以這種連接可以認(rèn)為是暫時的,感興趣的朋友一起看看吧2024-01-01
從基礎(chǔ)到高級詳解Python中文件操作的全攻略實戰(zhàn)指南
在Python編程中,文件操作是連接程序與外部存儲的橋梁,本文將從基礎(chǔ)讀寫講起,逐步深入到高效處理、異常管理、二進(jìn)制操作等高級場景,希望對大家有所幫助2025-10-10

