Easy Tutorial
❮ Python Check Is Number Python Bubble Sort ❯

Python complex() Function

Python Built-in Functions


Description

The complex() function is used to create a complex number with the value real + imag * j or convert a string or number to a complex number. If the first parameter is a string, it should not specify the second parameter.

Syntax

The syntax for complex is:

class complex([real[, imag]])

Parameter details:

Return Value

Returns a complex number.

Examples

The following examples demonstrate the usage of complex:

>>> complex(1, 2)
(1 + 2j)

>>> complex(1)    # Number
(1 + 0j)

>>> complex("1")  # Treated as a string
(1 + 0j)

# Note: There should be no spaces around the "+" sign, it should be "1+2j" instead of "1 + 2j", otherwise it will raise an error
>>> complex("1+2j")
(1 + 2j)

Python Built-in Functions

❮ Python Check Is Number Python Bubble Sort ❯