Python complex() Function
Description
complex()This function is used to create a complex number with the value real + imag * j, or to convert a string or number into a complex number. If the first argument is a string, the second parameter does not need to be specified.
Syntax
complex syntax:
class complex([real[, imag]])
Parameter Description:
- real -- int, long, float, or string;
- imag -- int, long, float;
Return Value
Returns a complex number.
Examples
The following examples demonstrate the usage of complex():
>>>complex(1, 2)
(1 + 2j)
>>> complex(1) # number
(1 + 0j)
>>> complex("1") # treated as a string
(1 + 0j)
# Note: There must be no spaces on either side of the "+" sign. That is, it cannot be written as "1 + 2j", it should be "1+2j", otherwise an error will be reported
>>> complex("1+2j")
(1 + 2j)
Python Built-in Functions