{"id":24798,"date":"2021-11-02T09:47:48","date_gmt":"2021-11-02T04:17:48","guid":{"rendered":"https:\/\/python-programs.com\/?p=24798"},"modified":"2021-11-05T20:35:58","modified_gmt":"2021-11-05T15:05:58","slug":"python-complex-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-complex-method-with-examples\/","title":{"rendered":"Python complex() Method with Examples"},"content":{"rendered":"

In the previous article, we have discussed Python bin() Method with Examples<\/a>
\ncomplex() Method in Python:<\/strong><\/p>\n

By specifying a real number and an imaginary number, the complex() function returns a complex number.<\/p>\n

Syntax:<\/strong><\/p>\n

complex(real, imaginary)<\/pre>\n

Parameters<\/strong><\/p>\n

real:<\/strong> This is Required. A number that represents the complex number’s real part. 0 is the default. The real number can also be a String, such as ‘2+6j’; in this case, the second parameter should be omitted.<\/p>\n

imaginary:<\/strong> This is Optional. A number that represents the complex number’s imaginary part. 0 is the default.<\/p>\n

Return Value:<\/strong><\/p>\n

The complex() method, as the name implies, returns a complex number.<\/p>\n

The ValueError exception is thrown if the string passed to this method is not a valid complex number.<\/p>\n

Note:<\/strong> Complex() always expects a string of the form real+imaginaryj<\/strong> or real+imaginaryJ<\/strong>.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

real number = 2\r\nimaginary number = 4<\/pre>\n

Output:<\/strong><\/p>\n

The complex number for the given real and imaginary numbers{ 2 , 4 }=  (2+4j)<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

real number = 3<\/pre>\n

Output:<\/strong><\/p>\n

The complex number for the given real part{ 3 }=  (3+0j)<\/pre>\n

complex() Method with Examples in Python<\/h2>\n