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
In general, complex( ) function takes two parameters:
- real (optional): The real part of the complex number. Default is
- imag (optional): The imaginary part of the complex number. Default is
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
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
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
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
Creating a Full Complex Number:
You can specify both the real and imaginary parts of the complex number by providing two arguments to the
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
Nuances to Remember:
- The real and imaginary parts can be of type
- If you provide no arguments, the
complex()function will return
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
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.
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>: This is the real part of the complex number.
<operator>: It can be either
-(minus) to indicate the sign of the imaginary part.
<imag>: This represents the imaginary part of the complex number.
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
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
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
# This will raise an error complex("2+3j", 2)
j in Python
In Python, the imaginary unit is represented by
J), unlike the traditional mathematical notation of
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.
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.
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.
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.
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
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.