Understanding Python complex()
Function
Python complex() function is a built-in function that creates complex numbers. A complex number is a number with a real part and an imaginary part, usually written in the form a + bj, where a is the real part, b is the imaginary part, and j is the imaginary unit (the square root of -1). You can use the complex() function to create a complex number by providing the real and imaginary parts as arguments. If you provide only one argument, it’s considered as the real part, and the imaginary part is set to 0.
Syntax of Python complex
()
complex
()
complex_number = complex(real, imaginary)
Explanation
complex_number
: Variable will store the complex number created by the function.complex()
: Built-in function that creates a complex number.real
(optional): Real part of the complex number. If not provided, it defaults to 0.imaginary
(optional): Imaginary part of the complex number. If not provided, it defaults to 0.
Example of Python complex
()
complex
()
z = complex(2, 3)
print(z)
Explanation
z = complex(2, 3)
: Creates a complex number with a real part of 2 and an imaginary part of 3 and assigns it toz
.print(z)
: Prints the complex numberz
.
Output
(2+3j)
complex()
Parameters
Python complex() function accepts up to two parameters, both of which are optional. The first parameter is the real part of the complex number, and the second is the imaginary part. If you don’t provide either parameter, complex() returns a complex number with a real part of 0 and an imaginary part of 0j. If you give only the first parameter, complex() treats it as the real part and sets the imaginary part to 0. Both parameters can be integers, floats, or even numeric strings.
Syntax
complex_number = complex(real, imaginary)
Example
z1 = complex(5)
z2 = complex()
print(z1)
print(z2)
Explanation
z1 = complex(5)
: Creates a complex number with a real part of 5 and an imaginary part of 0, and assigns it toz1
.z2 = complex()
: Creates a complex number with a real part of 0 and an imaginary part of 0, and assigns it toz2
.print(z1)
: Prints the complex numberz1
.print(z2)
: Prints the complex numberz2
.
Output
(5+0j)
0j
complex()
Return Value
Python complex() function always returns a complex number. If you provide both the real and imaginary parts, it returns a complex number with those values. If you provide only the real part, it returns a complex number with that real part and an imaginary part of 0j. If you don’t give any arguments, it returns 0j, a complex number with real and imaginary parts set to 0.
Example
z = complex(2, -4)
print(z)
Explanation
z = complex(2, -4)
: Creates a complex number with a real part of 2 and an imaginary part of -4, and assigns it toz
.print(z)
: Prints the complex numberz
.
Output
(2-4j)
Create Complex Number Without Using Python complex()
You can create a complex number in Python even without explicitly using the Python complex() function. You do this by directly writing the number a + bj, where a is the real part, b is the imaginary part, and j (or J) is the imaginary unit. Python automatically interprets this as a complex number.
Example
z = 4 + 7j
print(z)
Explanation
z = 4 + 7j
: Creates a complex number with a real part of 4 and an imaginary part of 7, and assigns it toz
.print(z)
: Prints the complex numberz
.
Output
(4+7j)
How to Create a Complex Number in Python?
You can use the Python complex() function to create a complex number in Python or directly write the number as a + bj (or a + bJ). With complex(), you provide the real and imaginary parts as arguments. When using the direct form, you write the real part, a plus sign, the imaginary part, and append j or J to indicate the imaginary unit.
Syntax
# Method 1: Using complex()
complex_number = complex(real, imaginary)
# Method 2: Direct form
complex_number = real + imaginaryj
Explanation
# Method 1: Using complex()
: Indicates the first method of creating complex number.complex_number = complex(real, imaginary)
: Creates a complex number using thecomplex()
function.# Method 2: Direct form
: Indicates the second method of creating complex number.complex_number = real + imaginaryj
: Creates a complex number directly using thej
suffix.complex_number
: Variable will store the resulting complex number, created using either method.real
: Represents the real part of the complex number.imaginary
: Represents the imaginary part of the complex number.j
: The imaginary unit in Python (you can also useJ
).
Example
z1 = complex(3, 4)
z2 = 6 - 2j
print(z1)
print(z2)
Explanation
z1 = complex(3, 4)
: Creates a complex number usingcomplex()
with a real part of 3 and an imaginary part of 4, assigned toz1
.z2 = 6 - 2j
: Directly creates a complex number with a real part of 6 and an imaginary part of -2, assigned toz2
.print(z1)
: Prints the complex numberz1
.print(z2)
: Prints the complex numberz2
.
Output
(3+4j)
(6-2j)
complex()
With Integer and Float Type Parameters
Python complex() function can accept integer and float type parameters for the real and imaginary parts. When you pass integers, they are treated as whole numbers. When you pass floats, they are treated as numbers with a decimal point. Python correctly interprets both types and creates a complex number accordingly.
Example
z1 = complex(2, 3) # Integer arguments
z2 = complex(2.5, 3.5) # Float arguments
print(z1)
print(z2)
Explanation
z1 = complex(2, 3)
: Creates a complex number with an integer real part (2) and an integer imaginary part (3).z2 = complex(2.5, 3.5)
: Creates a complex number with a float real part (2.5) and a float imaginary part (3.5).print(z1)
: Prints the complex numberz1
.print(z2)
: Prints the complex numberz2
.
Output
(2+3j)
(2.5+3.5j)
Python complex()
from String Representation
With certain limitations, you can use the Python complex() function to create a complex number from a string representation. The input string must be in a valid numeric format that Python can interpret as a number. You can only pass one parameter in this case, which will be considered a real part of the complex number.
Syntax
complex_number = complex(string)
Explanation
complex_number
: Variable where the resulting complex number will be stored.complex()
: Function creates a complex number from the given string. You can only pass one parameter tocomplex()
in this case.string
: String representation of a number (e.g., “5”, “3.14”). It is used as input for thecomplex()
function.
Example
z = complex("7")
print(z)
Explanation
z = complex("7")
: Creates a complex number from the string “7”, which is treated as the real part, with an imaginary part of 0.print(z)
: Prints the complex numberz
.
Output
(7+0j)
complex()
with String Type Parameters of Complex Number Form
Python complex() function can also accept a single string parameter in the form of a complex number, like “a+bj” or “a-bj”, where “a” and “b” are numeric parts (integer or float) and “j” is the imaginary unit. In this case, Python will parse the string and extract the real and imaginary parts to create a complex number. You can’t pass two-string arguments in this form. It’s important that the string strictly adheres to the complex number format without any extra spaces.
Example
z1 = complex("1+2j")
z2 = complex("3-4j")
print(z1)
print(z2)
Explanation
z1 = complex("1+2j")
: Creates a complex number from the string “1+2j”, with a real part of 1 and an imaginary part of 2.z2 = complex("3-4j")
: Creates a complex number from the string “3-4j”, with a real part of 3 and an imaginary part of -4.print(z1)
: Prints the complex numberz1
.print(z2)
: Prints the complex numberz2
.
Output
(1+2j)
(3-4j)
complex()
With String Type Parameters of the Numeric Form
The strings must be in a valid numeric form when using Python complex() with string type parameters. This means they should represent either an integer or a floating-point number. You can pass one or two string arguments. Passing one string is treated as the real part, and the imaginary part is set to 0. If you pass two strings, they are treated as the real and imaginary parts, respectively.
Syntax
complex_number = complex(real_string, imaginary_string)
Explanation
complex_number
: Variable will store the resulting complex number.complex()
: Function creates a complex number from the given strings.real_string
: String representing the real part (e.g., “2” or “3.14”). It is used as input for thecomplex()
function.imaginary_string
: String representing the imaginary part (e.g., “5” or “-2.7”). It is used as input for thecomplex()
function.
Example
z1 = complex("4", "2")
z2 = complex("1.5", "-2.5")
print(z1)
print(z2)
Explanation
z1 = complex("4", "2")
: Creates a complex number with a real part of 4 and an imaginary part of 2 from the given strings.z2 = complex("1.5", "-2.5")
: Creates a complex number with a real part of 1.5 and an imaginary part of -2.5 from the given strings.print(z1)
: Prints the complex numberz1
.print(z2)
:Prints the complex numberz2
.
Output
(4+2j)
(1.5-2.5j)
Conclusion
Python complex() is a function for creating and working with complex numbers in Python. It can handle various types of inputs, including integers, floats, and even strings in specific formats. Understanding how to use Python complex with different parameter types and how it interprets string representations is crucial for effectively using complex numbers in your programs. You can easily create a complex number using complex() or j in your code. Whether you’re performing mathematical computations, working with signal processing, or solving problems in physics or engineering, complex() provides a simple and efficient way to generate complex numbers.