Python built-in functions complex detailed _python

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 parameter is a string, it would be interpreted as a complex number and the function must be called without a s Econd 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-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 "the". For example, complex (' 1+2j ') is fine, but complex (' 1 + 2j ') raises valueerror.

Description

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

2. When two parameters are not provided, return the complex number 0j.

>>> complex ()
0j
 

3. When the first argument is a string, the second argument cannot be supplied at the time of the call. At this point, the string argument needs to be a string that can represent a complex number, and no spaces can appear around the plus or minus signs.

>>> complex (' 1+2j ', 2) #第一个参数为字符串, cannot accept the second argument
Traceback (most recent call last):
 File "<pyshell#2 > ", Line 1, in <module>
  complex (' 1+2j ', 2)
Typeerror:complex () can ' t take second arg if a string

>>> Complex (' 1 + 2j ') #不能有空格
Traceback (most recent):
 File "<pyshell#3>", line 1, in <module>
  complex (' 1 + 2j ')
V Alueerror:complex () arg is a malformed string

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

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

Thank you for reading, I hope to help you, thank you for your support for this site!

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.