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.
說(shuō)明:
1. 函數(shù)功能,返回一個(gè)復(fù)數(shù)。有兩個(gè)可選參數(shù)。
2. 當(dāng)兩個(gè)參數(shù)都不提供時(shí),返回復(fù)數(shù) 0j。
>>> complex() 0j
3. 當(dāng)?shù)谝粋€(gè)參數(shù)為字符串時(shí),調(diào)用時(shí)不能提供第二個(gè)參數(shù)。此時(shí)字符串參數(shù),需是一個(gè)能表示復(fù)數(shù)的字符串,而且加號(hào)或者減號(hào)左右不能出現(xiàn)空格。
>>> complex('1+2j',2) #第一個(gè)參數(shù)為字符串,不能接受第二個(gè)參數(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ù)谝粋€(gè)參數(shù)為int或者float時(shí),第二個(gè)參數(shù)可為空,表示虛部為0;如果提供第二個(gè)參數(shù),第二個(gè)參數(shù)也需為int或者float。
>>> complex(2) (2+0j) >>> complex(2.1,-3.4) (2.1-3.4j)
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Python實(shí)現(xiàn)遞歸遍歷文件夾并刪除文件
本文給大家匯總了3個(gè)Python實(shí)現(xiàn)遍歷文件夾并刪除的代碼,主要是給大家分享下這3種方法的實(shí)現(xiàn)思路,有需要的小伙伴可以參考下2016-04-04Python?任務(wù)自動(dòng)化工具nox?的配置與?API詳情
這篇文章主要介紹了Python?任務(wù)自動(dòng)化工具nox?的配置與?API詳情,Nox?會(huì)話是通過(guò)被@nox.session裝飾的標(biāo)準(zhǔn)?Python?函數(shù)來(lái)配置的,具體詳情下文相關(guān)介紹需要的小伙伴可以參考一下2022-07-07Python+OpenCV六種實(shí)時(shí)圖像處理詳細(xì)講解
OpenCV常用的圖像處理為閾值二值化、邊緣檢測(cè)、輪廓檢測(cè)、高斯濾波、色彩轉(zhuǎn)換、調(diào)節(jié)對(duì)比度。本文主要介紹了利用Python和OpenCV對(duì)實(shí)時(shí)圖像進(jìn)行上述六種操作的詳細(xì)講解,感興趣的可以了解一下。2021-11-11解決List.append()?在?Python?中不起作用的問(wèn)題
在?Python?中,我們通常使用?List.append()?方法向列表末尾添加元素,然而,在某些情況下,你可能會(huì)遇到?List.append()?方法不起作用的問(wèn)題,本文將詳細(xì)討論這個(gè)問(wèn)題并提供解決方法,需要的朋友可以參考下2023-06-06OpenCV基礎(chǔ)操作指南之圖片的讀取與寫(xiě)出
圖像處理依賴于得到一幅圖像(例如,一張照片和一個(gè)視頻幀)并通過(guò)應(yīng)用信號(hào)處理技術(shù)的“播放”(playing)來(lái)得到預(yù)期的結(jié)果,這篇文章主要給大家介紹了關(guān)于OpenCV基礎(chǔ)操作指南之圖片讀取與寫(xiě)出的相關(guān)資料,需要的朋友可以參考下2022-01-01