Python complex() Function
complex()Is a built-in function in Python used to create complex numbers.
Complex numbers consist of a real part and an imaginary part, and are widely used in mathematics and engineering fields, such as signal processing, physics simulation, etc. Python natively supports the complex number type, making complex calculations more convenient.
Word Meaning: complexMeans "complex," referring to a number that contains a real part and an imaginary part.
Basic Syntax and Parameters
complex()Is a built-in function that can be called directly.
Syntax
complex(real) complex(real, imag) complex(string)
Parameter Description
- Parameter real:
- Type: number (integer or float)
- Description: The real part of the complex number. If this is the only parameter, it can be a real number or a string representing a complex number.
- Parameter imag(optional):
- Type: number (integer or float)
- Description: The imaginary part of the complex number.
- Parameter string:
- Type: string
- Description: A string representing a complex number, such as "3+4j" or "3+4i".
Function Description
- Return value: Returns a complex object of the form real + imag*j.
- Imaginary unit: Python uses
jto represent the imaginary unit (instead of the commonly used in physicsi)。
Examples
Let's master through examplescomplex()the usage of.
Example 1: Basic Usage - Creating Complex Numbers
Example
c1 = complex(3, 4)
print(c1) # Output: (3+4j)
# Imaginary part is 0
c2 = complex(5, 0)
print(c2) # Output: (5+0j)
# Real part is 0
c3 = complex(0, 2)
print(c3) # Output: 2j
# Negative imaginary part
c4 = complex(1, -2)
print(c4) # Output: (1-2j)
Expected output:
(3+4j) (5+0j) 2j (1-2j)
Code analysis:
complex(3, 4)Create a complex number with real part 3 and imaginary part 4.- Python uses
jinstead ofito represent the imaginary unit. - You can create pure imaginary numbers (real part 0) or pure real numbers (imaginary part 0).
Example 2: Creating Complex Numbers from Strings
Example
c1 = complex("3+4j")
print(c1) # Output: (3+4j)
# Spaces are also allowed
c2 = complex(" 1 + 2j ")
print(c2) # Output: (1+2j)
# Use i instead of j (only in strings)
c3 = complex("3+4j") # Python only recognizes j, not i
print(c3) # Output: (3+4j)
Expected output:
(3+4j) (1+2j) (3+4j)
Code analysis:
- A complex number in string format can be passed directly to the
complex()function. - Spaces are allowed in the string.
- Note: Python complex numbers only support
j, do not supporti。
Example 3: Operations on Complex Numbers
Example
a = complex(3, 4)
b = complex(1, 2)
# Addition
print(f"a + b = {a + b}") # Output: a + b = (4+6j)
# Subtraction
print(f"a - b = {a - b}") # Output: a - b = (2+2j)
# Multiplication
print(f"a * b = {a * b}") # Output: a * b = (-5+10j)
# Division
print(f"a / b = {a / b}") # Output: a / b = (2.2-0.4j)
# Conjugate complex number
print(f"Conjugate: {a.conjugate()}") # Output: Conjugate: (3-4j)
# Magnitude (absolute value)
print(f"Modulus: {abs(a)}") # Output: Modulus: 5.0
Expected output:
a + b = (4+6j) a - b = (2+2j) a * b = (-5+10j) a / b = (2.2-0.4j) 共轭: (3-4j) 模: 5.0
This example demonstrates the basic operations on complex numbers, including addition, subtraction, multiplication, division, conjugation, and modulus.
Other Extensions
Python3 Built-in Functions