Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Python – Convert String/Number to Complex Number – complex() Function with Examples

complex() function is used to find the a complex number in Python for the real and imaginary parts as the given inputs.
complex() function is a part of Python Built-in Functions.


Syntax of complex() Function

The syntax of complex() function in Python is:


Parameters of complex() Function in Python

complex() takes two parameters.
real – Where real is a real part and its default value is 0 if real is omitted.
imag – Where imag is a imaginary part and its default value is also 0 if imag is omitted.



complex() function is available and compatible with both Python 2.x and 3.x.


Return Value of complex() Function in Python

complex() function returns a complex number with the value in the form of real + imag*1j for the given inputs.


complex() Function Exception

complex() function throw a ValueError exception if the passed string is not a valid complex number.


Python complex() Function Example 1



Python complex() Function Example 2

x = (9+2j)
Type of x is <class 'complex'>
y = (-0-6j)
Type of y is <class 'complex'>
z = 0j
Type of z is <class 'complex'>

LearnAndLearn.com © 2018 - All rights reserved