complex() Function in Python

Python complex() Function

The complex() function in Python is used to create a complex number object. It takes two optional arguments, real and imaginary, which represent the real and imaginary parts of the complex number, respectively. If both arguments are omitted, the function returns a complex number with a real part of 0 and an imaginary part of 0.

Here's the general syntax of the complex() function:

complex(real, imaginary)

Examples:

Creating a complex number with real and imaginary parts:

z = complex(3, 4)
print(z)  # Output: (3+4j)

Creating a complex number with only the real part:

z = complex(5)
print(z)  # Output: (5+0j)

Creating a complex number with only the imaginary part:

z = complex(imaginary=2)
print(z)  # Output: 2j

Creating a complex number with no arguments (both real and imaginary parts are 0):

z = complex()
print(z)  # Output: 0j

Note that the j notation is used to represent the imaginary unit in Python.