Function Details: complex

Description


Creates a complex number with the value real + imag*1j or convert a string or number to a complex number.


Extended Description


The complex() function creates a complex number or converts a string or number to a complex number. It can be called with zero, one, or two arguments. If called with no arguments, it returns 0j. If called with one argument, it can be a string representing a complex number or a number which will be converted to a complex number with zero imaginary part.


Exceptions


  • ValueError: If the string passed as 'real' is not a valid complex number representation
  • TypeError: If incompatible types are provided for real or imag

Read More about complex from Python Documentation

Function Signature


complex(real: Union[int, float, str] = 0, imag: Union[int, float] = 0) -> complex

Module: builtins

Parameters


real: The real part of the complex number. Can be an int, float, or str. imag: The imaginary part of the complex number. Can be an int or float.


Parameter List


  • real: Union[int, float, str]
  • imag: Union[int, float]

Return


Returns a complex number.


Return Type


complex

Output

Explanation

This example shows different ways to create complex numbers using the complex() function.