Home »
Python
complex() function with example in Python
Python complex() function: Here, we are going to learn about the complex() function in Python with example.
Submitted by IncludeHelp, on April 03, 2019
Python complex() function
complex() function is a library function in Python, it is used to get the complex number from given a real number or/and an imaginary number (which is an optional part), it accepts either a real number only or read and an imaginary number and returns a complex number.
Syntax:
complex(real [,Imaginary])
Parameter(s):
- real – value of a real number
- Imaginary – an optional parameter, it's default value is 0.
Return value: complex – it returns a complex type of number containing the value in the form of (real + imaginary*j).
Example:
Input:
real = 10
img = 20
print(complex(real))
print(complex(real, img))
Output:
(10+0j)
(10+20j)
Python code to get the complex number
# python code to demonstrate example
# of complex() function
real = 10
img = 20
# complex number by passing real number
print(complex(real))
# complex number by passing real & img. number
print(complex(real, img))
# complex number by passing real part as 0
print(complex(0))
# complex number by passing real and img. part as 0, 0
print(complex(0,0))
# with float values
real = 10.23
img = 20.45
# complex number by passing real number
print(complex(real))
# complex number by passing real & img. number
print(complex(real, img))
Output
(10+0j)
(10+20j)
0j
0j
(10.23+0j)
(10.23+20.45j)
ADVERTISEMENT
ADVERTISEMENT