Python complex() Function
complex() is a built-in Python function that creates complex numbers.
A complex number consists of a real part and an imaginary part and is widely used in mathematics and engineering, such as signal processing and physical simulation. Python natively supports complex numbers, making complex calculations more convenient.
Word meaning: complex means "complex", denoting a number with a real part and an imaginary part.
Basic syntax and parameters
complex() is a built-in function and can be called directly.
Syntax format
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 it is the only argument, 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 in the form real + imag*j.
- Imaginary unit: Python uses
jto denote the imaginary unit (rather thani)。
Example
Let's master it through examples complex() through a series of examples from simple to complex.
Example 1: basic usage - creating complex numbers
Example
Create a complex number with two arguments: real part and imaginary part
c1
=
complex
(
3
,
4
)
(
c1
)
Output: (3+4j)
Imaginary part is 0
c2
=
complex
(
5
,
0
)
(
c2
)
Output: (5+0j)
Real part is 0
c3
=
complex
(
0
,
2
)
(
c3
)
Output: 2j
Negative imaginary part
c4
=
complex
(
1
,
2
)
(
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 denote the imaginary unit. - Pure imaginary numbers (real part 0) or pure real numbers (imaginary part 0) can be created.
Example 2: creating complex numbers from strings
Example
Create a complex number from a string
c1
=
complex
(
"3+4j"
)
(
c1
)
Output: (3+4j)
Spaces are allowed too
c2
=
complex
(
" 1 + 2j "
)
(
c2
)
Output: (1+2j)
Use i instead of j (only in strings)
c3
=
complex
(
"3+4j"
)
Python only recognizes j, not i
(
c3
)
Output: (3+4j)
Expected output:
(3+4j)
(1+2j)
(3+4j)
Code analysis:
- Complex numbers in string format can be passed directly to
complex()function in Vue 3. - Strings may contain spaces.
- Note: Python complex numbers only support
j, noti。
Example 3: operations on complex numbers
Example
Creating complex numbers
a
=
complex
(
3
,
4
)
b
=
complex
(
1
,
2
)
Addition
(
f
"a + b = {a + b}"
)
Output: a + b = (4+6j)
Subtraction
(
f
"a - b = {a - b}"
)
Output: a - b = (2+2j)
Multiplication
(
f
"a * b = {a * b}"
)
Output: a * b = (-5+10j)
Division
(
f
"a / b = {a / b}"
)
Output: a / b = (2.2-0.4j)
Conjugate complex number
(
f
"Conjugate: {a.conjugate()}"
)
Output: Conjugate: (3-4j)
Modulus (absolute value)
(
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 shows the basic operations on complex numbers, including addition, subtraction, multiplication, division, conjugate, and modulus.
Python3 built-in function