Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python complex() Function

Python complex() Function

Python3 Built-in Functions Python3 built-in 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

- 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. - Type: number (integer or float) - Description: the imaginary part of the complex number. - Type: string - Description: a string representing a complex number, such as "3+4j" or "3+4i".

Function description


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

)

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:

  1. complex(3, 4) Create a complex number with real part 3 and imaginary part 4.
  2. Python uses j instead of i to denote the imaginary unit.
  3. 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"

)

print

(

c1

)

         

Output: (3+4j)

Spaces are allowed too

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:

Example 3: operations on complex numbers

Example

Creating complex numbers

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)

Modulus (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 shows the basic operations on complex numbers, including addition, subtraction, multiplication, division, conjugate, and modulus.


Python3 Built-in Functions Python3 built-in function