{"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> By specifying a real number and an imaginary number, the complex() function returns a complex number.<\/p>\n Syntax:<\/strong><\/p>\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 Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Similarly, check for the other values<\/p>\n without giving imaginary part<\/strong><\/p>\n Output:<\/strong><\/p>\n without giving any parameters<\/strong><\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n In the previous article, we have discussed Python bin() Method with Examples complex() Method in Python: By specifying a real number and an imaginary number, the complex() function returns a complex number. Syntax: complex(real, imaginary) Parameters real: This is Required. A number that represents the complex number’s real part. 0 is the default. The real …<\/p>\n
\ncomplex() Method in Python:<\/strong><\/p>\ncomplex(real, imaginary)<\/pre>\n
real number = 2\r\nimaginary number = 4<\/pre>\n
The complex number for the given real and imaginary numbers{ 2 , 4 }= (2+4j)<\/pre>\n
real number = 3<\/pre>\n
The complex number for the given real part{ 3 }= (3+0j)<\/pre>\n
complex() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the number(real part) as static input and store it in a variable.\r\nreal_numbr = 2\r\n# Give the other number (imaginary part) as static input and store it in\r\n# another variable.\r\nimaginry_numbr = 4\r\n# Pass the given two numbers (real & imaginary parts) as the arguments to the\r\n# complex() function to get the complex number for the given real and\r\n# imaginary numbers.\r\n# Store it in another variable.\r\nrslt = complex(real_numbr, imaginry_numbr)\r\n# Print the complex number for the given real and imaginary numbers.\r\nprint(\r\n \"The complex number for the given real and imaginary numbers{\", real_numbr, \",\", imaginry_numbr, \"}= \", rslt)\r\n<\/pre>\n
The complex number for the given real and imaginary numbers{ 2 , 4 }= (2+4j)<\/pre>\n
real_numbr = 3\r\nrslt = complex(real_numbr)\r\nprint(\r\n \"The complex number for the given real part{\", real_numbr, \"}= \", rslt)\r\n<\/pre>\n
The complex number for the given real part{ 3 }= (3+0j)<\/pre>\n
rslt = complex()\r\nprint(\"The complex number without giving any parameters = \", rslt)\r\n<\/pre>\n
The complex number without giving any parameters = 0j<\/pre>\n
When a number is given as a string?<\/h4>\n
gvn_numbr = \"7+8j\"\r\nrslt = complex(gvn_numbr)\r\nprint(\r\n \"The complex number for the given number {\", gvn_numbr, \"} = \", rslt)\r\n<\/pre>\n
The complex number for the given number { 7+8j } = (7+8j)<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the number(real part) as user input using the int(input()) function and store it in a variable.\r\nreal_numbr = int(input(\"Enter some random number = \"))\r\n# Give the other number (imaginary part) as user input using the int(input()) function \r\n# and store it in another variable.\r\nimaginry_numbr = int(input(\"Enter some random number = \"))\r\n# Pass the given two numbers (real & imaginary parts) as the arguments to the\r\n# complex() function to get the complex number for the given real and\r\n# imaginary numbers.\r\n# Store it in another variable.\r\nrslt = complex(real_numbr, imaginry_numbr)\r\n# Print the complex number for the given real and imaginary numbers.\r\nprint(\r\n \"The complex number for the given real and imaginary numbers{\", real_numbr, \",\", imaginry_numbr, \"}= \", rslt)\r\n<\/pre>\n
Enter some random number = 5\r\nEnter some random number = 6\r\nThe complex number for the given real and imaginary numbers{ 5 , 6 }= (5+6j)<\/pre>\n
\n