Python built-in function complex, pythoncomplex

Source: Internet
Author: User

Python built-in function complex, pythoncomplex

English document:

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.

Note:

1. The function returns a plural number. There are two optional parameters.

2. If neither of the two parameters is provided, return the plural 0j.

>>> complex()0j 

3. When the first parameter is a string, the second parameter cannot be provided during the call. In this case, the string parameter must be a string that can represent the plural number, and no space is allowed between the plus sign or minus sign.

>>> Complex ('1 + 2j', 2) # The first parameter is a string and cannot accept the second parameter 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') # There cannot be spaces Traceback (most recent call last ): file "<pyshell #3>", line 1, in <module> complex ('1 + 2j') ValueError: complex () arg is a malformed string

4. When the first parameter is int or float, the second parameter can be blank, indicating that the virtual part is 0. If the second parameter is provided, the second parameter also needs to be int or float.

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

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.