complex() Builtin Function


Python - complex()

Python complex() builtin function is used to define a complex number with real and imaginary parts.

complex() returns a complex number created using the given real and/or imaginary parts as arguments.

In this tutorial, you will learn the syntax of complex() function, and then its usage with the help of example programs.

Syntax

The syntax of complex() function is

complex(real, imaginary)

where

ParameterDescription
realAn integer, or decimal point value.
imaginaryAn integer, or decimal point value.

Examples

1. 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}')

Output

complex() : (5+8j)

2. 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}')

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.