Python 內建函數complex詳解

來源:互聯網
上載者:User
英文文檔:

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. 函數功能,返回一個複數。有兩個選擇性參數。

  2. 當兩個參數都不提供時,返回複數 0j。

>>> complex()0j 

  3. 當第一個參數為字串時,調用時不能提供第二個參數。此時字串參數,需是一個能表示複數的字串,而且加號或者減號左右不能出現空格。

>>> complex('1+2j',2) #第一個參數為字串,不能接受第二個參數Traceback (most recent call last): File "", line 1, in   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 "", line 1, in   complex('1 + 2j')ValueError: complex() arg is a malformed string

  4. 當第一個參數為int或者float時,第二個參數可為空白,表示虛部為0;如果提供第二個參數,第二個參數也需為int或者float。

>>> complex(2)(2+0j)>>> complex(2.1,-3.4)(2.1-3.4j) 

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.