Tutorial Study Image

Python complex()

The complex function can be used in two ways in python. Two numbers can be given as parameters and the function returns a complex number. It can also be used to convert a string to a complex number


complex(real, imaginary) #where real is the real part and imaginary is imaginary part of the complex number
 

complex() Parameters:

The complex function can take numbers as parameter and can create a complex number from it. It can also take a well formed string in the format of a complex number and can return correspondijng complex number

Parameter Description Required / Optional
real Default 0, the real part of complex number Required
imaginary The imaginary part of the complex number Optional
String If a well formed string is passed, it will be a single mandatory parameter and will be converted to a complex number. See Example 2 Optional

complex() Return Value

The return value will always be a complex number.

Input Return Value
None 0j
Real part only Complex number with real part given and imaginary 0
Real and imaginary part Complex number with real and imaginary as given
A string in complex number format Corresponding complex number

Examples of complex() method in Python

Example 1: Passing Numbers as parameter


x = complex(3.5, 5.8) 
print(x)
 

Output:

(3.5+5.8j)

Example 2: Passing string as parameter


x = complex('1+j') 
print(x)
 

Output:

(1+1j)