Python complex() Function

This article covers the use of the Python complex Function.

This article covers the use of the python complex function and it’s uses complete with examples.

A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers, and i is a solution of the equation x² = −1. Because no real number satisfies this equation, i is called an imaginary number.

The complex function takes integers or strings as inputs and returns an appropriate complex number. Attempting to pass a inappropriate value will possibly result in a ValueError.


complex(real, imag)

The complex() takes two parameters, real and imag.

  • real, The real part of the number. If left empty, will take the default value of 0.
  • imag, The imaginary part of the number. If left empty will take the default of 0.

You can also choose to pass a single string instead of two numbers. See the examples below for clarification.

Example 1

>>> complex(3)

>>> complex(0,7)

>>> complex(3,4)

>>> complex(-3,-2)

Example 2

>>> complex('3+4j')

>>> complex('5-2j')


The conjugate of a complex number can be thought of it’s opposite. The conjugate of a complex number is found by multiplying the imaginary part by the - sign. Any complex number returned from the complex() function can have the conjugate() function applied to it.

>>> a = complex(3+4j)
>>> a.conjugate()

>>> b = complex(5-1j)
>>> b.conjugate()

This marks the end of the Python complex Function. Any suggestions or contributions for CodersLegacy are more than welcome. Questions can be asked in the comment section below.

Here’s a link back to the main Python Built-in Functions page.

Notify of
Inline Feedbacks
View all comments