Python built-in function (--complex)

Source: Internet
Author: User

English documents:

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'll 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 is 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 is omitted, returns 0j .

Note

When converting from a string, the string must not contain whitespace around the central + or - operator. For example, was complex(‘1+2j‘) fine, but complex(‘1 + 2j‘) raises ValueError .

Description

1. function function, return a complex number. There are two optional parameters.

2. Returns a complex number 0j when none of the two parameters are available.

>>> Complex () 0j

3. When the first argument is a string, the second argument cannot be supplied when called. In this case, the string parameter is a string that can represent a complex number, and the plus or minus sign cannot have spaces around it.

>>> Complex ('1+2j', 2)#The first argument is a string and cannot accept the second argumentTraceback (most recent): File"<pyshell#2>", Line 1,inch<module>Complex ('1+2j', 2) Typeerror:complex () can'T take second ARG if first is a string>>> Complex ('1 + 2j')#cannot have spacesTraceback (most recent): File"<pyshell#3>", Line 1,inch<module>Complex ('1 + 2j') Valueerror:complex () Arg isA malformed string

4. When the first argument is an int or float, the second argument can be empty, indicating that the imaginary part is 0, and if the second argument is supplied, the second argument must be either int or float.

>>> Complex (2) (0j)>>> Complex (2.1,-3.4) (2.1-3.4j)

Python built-in function (--complex)

Related Article

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.