Python - complex()

The complex() function returns a complex number with real and imaginary component. The function can take two parameters, one each for real and imaginary component. They can be of any numeric type (int, float or complex)

>>>complex(4,5)
(4+5j)
>>>complex(1.5, -2.5)
(1.5-2.5j)
>>>complex(1.5j, 2.5j)
(-2.5+1.5j)

If only one parameter is given it is treated as real component, imaginary component is assumed to be zero.

>>>complex(5)
(5+0j)
>>>complex(1.5)
(1.5+0j)

First parameter can be a string containing digits, decimal point or scientific notation (E or e). In that case, imaginary component is assumed to be zero.

>>>complex('5')
(5+0j)
>>>complex('1.5')
(1.5+0j)
>>>complex('1e6')
(1000000+0j)

Learn Python using coding questions with answers.

Python Questions & Answers

Want to check how much you know Python?

Start Python Skill Test