complex() Builtin Function

Python – complex()

Python complex() builtin function returns a complex number created using the given real and/or imaginary parts.

Syntax

The syntax of complex() function is

complex(real, imaginary)

where

  • real is an integer.
  • imaginary is an integer.

Examples

complex() with real and imaginary parts

In the following program, we create a complex type object with real and imaginary parts passed as arguments to complex() function.

Python Program

real = 5
imaginary = 8
output = complex(real, imaginary)
print(f'complex() : {output}')
Run

Output

complex() : (5+8j)

complex() with real part only

In the following program, we create a complex type object by passing only real part as argument to complex() function.

The default value of imaginary parameter is 0. real parameter is mandatory.

Python Program

real = 5
output = complex(real)
print(f'complex() : {output}')
Run

Output

complex() : (5+0j)

Summary

In this tutorial of Python Examples, we learned the syntax of complex() function, and how to create a complex number, with examples.