complex() function in Python

The complex() function in Python constructs and returns an object of type complex - of the form x+iy, representing a complex number.

Built-in Functions Updated 3 Mar 2017 1 min read

Function Name

complex

Function Signature

class complex([real[, imag]])

Function Overview

  • The complex() function in Python is the constructor for the class complex and returns an object of type complex – representing complex numbers.
  • A complex number is of the form x+iy. Where,
    • x is the real part.
    • y is the imaginary part.
    • i is the equivalent of a imaginary one – which satisfies i2 = -1.
    • Both the real part and the imaginary part of a complex number are real numbers.

Parameters

real – The real part of the complex number

imag – The imaginary part of the complex number

Note: A complex number can be constructed by passing a string of the form

“<RealPart>+<ImaginaryPart>j”, where RealPart and ImaginaryPart are real numbers.

E.g., “20-1j”, “-20+2j”

If the string format like the ones specified above are passed the second parameter should not be passed to the function complex().

Example 1

python
# Define real and imaginary parts

real    = 10

imag    = -1

# Construct a complex number

c1      = complex(real, imag)

# Print the complex number

print(c1)

Output

Output
(10-1j)

Example 2

python
# Construct a complex number using a string

complexNumber = complex("20-1j")

# Print the complex number

print(complexNumber)

Output

Output
(20-1j)

##