{"id":4676,"date":"2020-03-31T18:19:57","date_gmt":"2020-03-31T18:19:57","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4676"},"modified":"2022-08-06T13:13:14","modified_gmt":"2022-08-06T13:13:14","slug":"python-complex-method","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/python-complex-method","title":{"rendered":"How to Use the Python complex() method"},"content":{"rendered":"\n<p>The Python <strong>complex()<\/strong> method is used to create a complex number from other primitive data-types. This is useful whenever you want to perform complex arithmetic and transformations quickly.<\/p>\n\n\n\n<p>Let&#8217;s take a look at how we can use this method.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Python complex()<\/h2>\n\n\n\n<p>This function returns a complex number, which contains a real part, and an imaginary part. The complex number is of the <code>class complex<\/code> datatype.<\/p>\n\n\n\n<p>Therefore, the function call syntax is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass complex(&#x5B;real&#x5B;, imag]])\n<\/pre><\/div>\n\n\n<p>The complex number will be of the form:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncomplex_number = real + imag*j\n<\/pre><\/div>\n\n\n<p>Here, <code>j<\/code> is the imaginary number (sqrt(-1))<\/p>\n\n\n\n<p>The Python <code>complex()<\/code> method will return this complex number.<\/p>\n\n\n\n<p>Let&#8217;s look at some examples, to get familiar with this method.<\/p>\n\n\n\n<p>You may think that we could use only integers and floats to construct the complex number, but that&#8217;s not that case!<\/p>\n\n\n\n<p>We can also construct it from a string, hexadecimal, binary too, or even from another complex number!<\/p>\n\n\n\n<p>We&#8217;ll take a look at some examples, to illustrate this point.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Python complex() method<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Calling complex() without any parameter<\/h3>\n\n\n\n<p>We can call this method without passing any arguments to it. This will return the zero complex number <code>0j<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = complex()\nprint(type(a))\nprint(a)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;class &#039;complex&#039;&gt;\n0j\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Calling Python complex() using numeric parameters<\/h3>\n\n\n\n<p>This will construct the desired complex number of the form <code>a + bj<\/code>, where <code>a<\/code> and <code>b<\/code> are the numeric parameters.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = complex(1, 2)\nprint(a)\n\nb = complex(1, 1.5)\nprint(b)\n\nc = complex(-1.5, 3.414)\nprint(c)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n(1+2j)\n(1+1.5j)\n(-1.5+3.414j)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Calling complex() with Hexadecimal \/ Binary arguments<\/h3>\n\n\n\n<p>We can also directly pass hexadecimal or binary number into this, without converting it to an integer.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = complex(0xFF)  # hexadecimal\nprint(a)\n\nb = complex(0b1010, -1)  # binary\nprint(b)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n(255+0j)\n(10-1j)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Calling complex() using another complex number<\/h3>\n\n\n\n<p>We can also pass another complex number, when constructing a complex number using <code>complex()<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = 1 + 2j\nb = complex(a, -4) # Construct using another complex number\nprint(b)\nc = complex(1+2j, 1+2J)\nprint(c)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n(1-2j)\n(-1+3j)\n<\/pre><\/div>\n\n\n<p>Since both the arguments are added according to the form <code>a + b*j<\/code>, in the first case, the result will be: 1 + 2j + (-4*j) = <strong>1 &#8211; 2j<\/strong><\/p>\n\n\n\n<p>In the second case, we have (since j*j = -1): 1+2j + (1+2j)*j = (1 + 2j + j &#8211; 2) = <strong>-1+3j<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Calling Python complex() using string arguments<\/h3>\n\n\n\n<p>We can also pass string with it, provided that it <strong>does not<\/strong> have any white space in between. The string must only be of the form &#8220;a+bj&#8221;, i.e, a string which can represent a complex number.<\/p>\n\n\n\n<p>If we are using a string, we can only use <strong>one argument<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = complex(&quot;1+2j&quot;)\nprint(a)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n(1+2j)\n<\/pre><\/div>\n\n\n<p>If the string has whitespaces or any other extraneous character, a <code>ValueError<\/code> exception will be raised.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nb = complex(&quot;2 + 4j&quot;)\nprint(b)\n<\/pre><\/div>\n\n\n<p>This will raise a <code>ValueError<\/code>, since there are spaces in the string.<\/p>\n\n\n\n<p>Similarly, if we pass two arguments to Python <code>complex()<\/code>, it will raise a <code>TypeError<\/code> exception, as we are not allowed to pass multiple arguments if the first argument is a String.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned how we can construct a complex number from different data-types, using the built-in <code>complex()<\/code> method. If you want to learn about other <a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\" class=\"rank-math-link\">Python built-in functions visit here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.python.org\/3.8\/library\/functions.html#complex\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Python Documentation<\/a> on the complex() method<\/li><li>JournalDev article on using the complex() method<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n","protected":false},"excerpt":{"rendered":"<p>The Python complex() method is used to create a complex number from other primitive data-types. This is useful whenever you want to perform complex arithmetic and transformations quickly. Let&#8217;s take a look at how we can use this method. Syntax of Python complex() This function returns a complex number, which contains a real part, and [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":4681,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-4676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=4676"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4676\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/4681"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}