Python complex() Function

Spread the love

Python complex() function is used to convert numbers or string into a complex number. The complex() method takes two parameters, namely the real and imaginary values.

complex( ) Syntax

complex([real[, imag]])

complex() Parameters

In general, complex( ) function takes two parameters:

  • real (optional): The real part of the complex number. Default is 0.
  • imag (optional): The imaginary part of the complex number. Default is 0.

If the first parameter passed to this method is a string, it will be interpreted as a complex number. In this case, the second parameter shouldn’t be passed.

complex( ) Return Value

As suggested by the name, complex( ) function returns a complex number.

Examples of Using the complex( ) Function

Creating a Pure Real Number:

When you provide a single numerical argument to the complex() function, it treats this number as the real part of the complex number, while the imaginary part defaults to 0.

x = complex(3)
print(x)          # Outputs: (3+0j)

In this example, 3 is the real part of the complex number. Since we didn’t specify an imaginary part, it defaults to 0, resulting in the complex number (3+0j).

Creating a Pure Imaginary Number:

If you want to create a complex number with only an imaginary part, you can set the real part to 0.

y = complex(0, 4)
print(y)          # Outputs: 4j

Here, the real part is 0, and the imaginary part is 4. Hence, the output is 4j. Notice how Python represents the imaginary unit as j (or J).

Creating a Full Complex Number:

You can specify both the real and imaginary parts of the complex number by providing two arguments to the complex() function.

z = complex(2, -3)
print(z)          # Outputs: (2-3j)

n this case, 2 is the real part and -3 is the imaginary part, resulting in the complex number (2-3j).

Nuances to Remember:

  • The real and imaginary parts can be of type int or float.
  • If you provide no arguments, the complex() function will return 0j.

Bonus: Using Floats:

You aren’t limited to integers when creating complex numbers. Floats can be used as well.

w = complex(3.5, -4.2)
print(w)          # Outputs: (3.5-4.2j)

This creates a complex number with a real part of 3.5 and an imaginary part of -4.2.

Convert a String into a Complex Number

The versatility of the complex() function lies not just in its ability to create complex numbers directly from numerical values, but also in its power to convert certain formats, especially strings, into complex numbers. Let’s dive deeper into this conversion process.

The complex() function can convert a string representation of a complex number into an actual complex number data type.

Syntax for String Conversion: The string format should look like "<real><operator><imag>j".

  • <real>: This is the real part of the complex number.
  • <operator>: It can be either + (plus) or - (minus) to indicate the sign of the imaginary part.
  • <imag>: This represents the imaginary part of the complex number.

Examples:

Conversion from a Standard Format:

s1 = "2+3j"
c1 = complex(s1)
print(c1)          # Outputs: (2+3j)

Here, the string “2+3j” represents a complex number with a real part of 2 and an imaginary part of 3.

Handling Negative Imaginary Parts:

s2 = "4-5j"
c2 = complex(s2)
print(c2)          # Outputs: (4-5j)

This time, the imaginary part is negative. The string “4-5j” is seamlessly converted to the complex number (4-5j).

Pure Imaginary Numbers in String Format:

s3 = "6j"
c3 = complex(s3)
print(c3)          # Outputs: 6j

Here, the string only specifies the imaginary part. The complex() function assumes the real part to be 0.

Behavior and Nuances

No String with Two Arguments

If the first argument is a string, the complex() function doesn’t accept the second argument. This will lead to a TypeError.

# This will raise an error
complex("2+3j", 2)

j in Python

In Python, the imaginary unit is represented by j (or J), unlike the traditional mathematical notation of i.

print(complex(0, 2))       # Outputs: 2j, NOT 2i

Mathematical Operations with Complex Numbers

Complex numbers, like other numerical data types in Python, can undergo various mathematical operations. Python’s innate support for complex numbers allows for the straightforward application of these operations. Let’s explore these in detail:

1. Addition (+):

Complex numbers can be added together by combining their real parts and their imaginary parts separately.

Example:

z1 = complex(2, 3)
z2 = complex(4, -1)

result = z1 + z2
print(result)  # Outputs: (6+2j)

In this case:

  • Real part: 2 (from z1) + 4 (from z2) = 6
  • Imaginary part: 3 (from z1) - 1 (from z2) = 2

2. Subtraction (-):

Similar to addition, subtraction involves taking the difference between the real parts and the imaginary parts separately.

Example:

z1 = complex(5, 4)
z2 = complex(2, 3)

result = z1 - z2
print(result)  # Outputs: (3+1j)

3. Multiplication (*):

Multiplying complex numbers is a bit trickier than adding or subtracting them. You’ll apply the distributive property.

Example:

z1 = complex(3, 2)
z2 = complex(1, 7)

result = z1 * z2
print(result)  # Outputs: (-11+23j)

4. Division (/):

Dividing complex numbers involves multiplying by the conjugate and can be a bit involved.

Example:

z1 = complex(5, 4)
z2 = complex(1, 2)

result = z1 / z2
print(result)  # Outputs: (2.6-1.2j)

Interactions with Other Built-in Functions

Python provides additional functions to work with complex numbers:

  • abs(): Returns the magnitude of the complex number.
  • real: A property to extract the real part.
  • imag: A property to extract the imaginary part.
z = complex(3, 4)
print(abs(z))              # Outputs: 5.0
print(z.real)              # Outputs: 3.0
print(z.imag)              # Outputs: 4.0

Conclusion

Python’s complex() function is a testament to the language’s comprehensive nature, catering to both the fundamental and advanced needs of programmers. Whether you’re a student exploring the fascinating world of complex numbers or a professional applying these numbers in real-world problems, Python’s native support streamlines the journey.

Leave a Reply