Complex() function in Python

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:

# Define real and imaginary parts

real    = 10

imag    = -1

 

# Construct a complex number

c1      = complex(real, imag)

 

# Print the complex number

print(c1)

 

Output:

(10-1j)

 

Example 2:

# Construct a complex number using a string

complexNumber = complex("20-1j")

 

# Print the complex number

print(complexNumber)

Output:

(20-1j)

 

 


Copyright 2024 © pythontic.com