{"id":122231,"date":"2020-12-18T18:10:56","date_gmt":"2020-12-18T18:10:56","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=18809"},"modified":"2020-12-18T18:10:56","modified_gmt":"2020-12-18T18:10:56","slug":"python-complex-a-useless-python-feature","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/12\/18\/python-complex-a-useless-python-feature\/","title":{"rendered":"Python complex() \u2014 A Useless Python Feature?"},"content":{"rendered":"<p class=\"has-pale-cyan-blue-background-color has-background\">The Python <code>complex()<\/code> method returns a complex number object. You can either pass a string argument to convert the string to a complex number, or you provide the real and imaginary parts to create a new complex number from those.<\/p>\n<p>This article shows you how to use Python&#8217;s built-in <code>complex()<\/code> constructor. You&#8217;ll not only learn how to use it&#8212;but also <strong><em>why it is useless<\/em><\/strong> and what you should do instead in newer Python versions. <\/p>\n<h2>Usage<\/h2>\n<p>Learn by example! Here are some examples of how to use the <code>complex()<\/code> <a href=\"https:\/\/blog.finxter.com\/python-built-in-functions\/\" title=\"Python Built-In Functions\">built-in function<\/a>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> complex(1, -2)\n(1-2j)\n>>> complex(2, -1)\n(2-1j)\n>>> complex(2, 2)\n(2+2j)\n>>> complex(1)\n(1+0j)\n>>> complex(2)\n(2+0j)\n>>> complex('42-21j')\n(42-21j)<\/pre>\n<\/p>\n<h2>Syntax Complex()<\/h2>\n<p>You can use the <code>complex()<\/code> method with three different argument lists. <\/p>\n<pre class=\"wp-block-preformatted\"><strong>Syntax: <\/strong>\n<code><strong>complex(real)<\/strong> # Imaginary Part is 0j<\/code>\n<code><strong>complex(real, img)<\/strong> # Both real and imaginary part are given<\/code>\n<code><strong>complex(string) <\/strong> # String has format 'x+yj' for real part x and imaginary part y. <\/code><\/pre>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<tbody>\n<tr>\n<td><strong>Arguments<\/strong><\/td>\n<td><code>real<\/code><\/td>\n<td>The real part of the complex number<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><code>img<\/code><\/td>\n<td>The imaginary part of the complex number<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><code>string<\/code><\/td>\n<td>A string defining the real and imaginary part in the form <code>'x+yj'<\/code> or <code>'x+yJ'<\/code> where <code>x<\/code> and <code>y<\/code> are integers for the real and imaginary parts.<\/td>\n<\/tr>\n<tr>\n<td><strong>Return Value<\/strong><\/td>\n<td><code>complex<\/code><\/td>\n<td>Returns a complex number.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2>Video Complex()<\/h2>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Python Complex() is Dead &#x1f632; Long Live Python Complex()!\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/1JE-5JK72yA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<h2>Interactive Shell Exercise: Understanding Complex()<\/h2>\n<p>Consider the following interactive code:<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/fdfd3b9a88\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"\" width=\"100%\" height=\"356\" frameborder=\"0\"><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: <\/em>Guess the output before running the code. <\/p>\n<hr class=\"wp-block-separator\"\/>\n<p><strong>But before we move on, I&#8217;m excited to present you my brand-new Python book <a rel=\"noreferrer noopener\" href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" title=\"https:\/\/amzn.to\/2WAYeJE\">Python One-Liners<\/a><\/strong> (Amazon Link).<\/p>\n<p>If you like one-liners, you&#8217;ll LOVE the book. It&#8217;ll teach you everything there is to know about a <strong>single line of Python code.<\/strong> But it&#8217;s also an <strong>introduction to computer science<\/strong>, data science, machine learning, and algorithms. <strong><em>The universe in a single line of Python!<\/em><\/strong><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" width=\"215\" height=\"283\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/02\/image-1.png\" alt=\"\" class=\"wp-image-5969\"\/><\/a><\/figure>\n<\/div>\n<p>The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). <\/p>\n<p>Link: <a href=\"https:\/\/nostarch.com\/pythononeliners\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/nostarch.com\/pythononeliners<\/a><\/p>\n<hr class=\"wp-block-separator\"\/>\n<h2>How to Create Complex Number Without complex()<\/h2>\n<p>Interestingly, you don&#8217;t need the <code>complex()<\/code> constructor to create a complex number! Instead, newer version of Python have <a href=\"https:\/\/blog.finxter.com\/python-built-in-functions\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Python Built-In Functions\">built-in<\/a> complex number support&#8212;just use the the syntax <code>x+yj<\/code> for real part <code>x<\/code> and imaginary part <code>y<\/code> to obtain a complex number.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">a = 1+1j\nb = 4+42j\nc = 0+0j print('Complex Numbers:')\nprint(a, b, c) print('Types:')\nprint(type(a), type(b), type(c))\n<\/pre>\n<p>The output is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Complex Numbers:\n(1+1j) (4+42j) 0j\nTypes:\n&lt;class 'complex'> &lt;class 'complex'> &lt;class 'complex'><\/pre>\n<\/p>\n<h2>Summary<\/h2>\n<p>The Python <code>complex()<\/code> method returns a complex number object. To create a complex number:<\/p>\n<ul>\n<li>Pass a string argument to convert the string to a complex number, or <\/li>\n<li>Provide the real and imaginary parts to create a new complex number from those.<\/li>\n<\/ul>\n<p>I hope you enjoyed the article! To improve your Python education, you may want to join the popular free <a href=\"https:\/\/blog.finxter.com\/email-academy\/\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Email Academy\">Finxter Email Academy<\/a>:<\/p>\n<hr class=\"wp-block-separator\"\/>\n<p>Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!<\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?<\/p>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n<\/p>\n<\/p>\n<p>The post <a href=\"https:\/\/blog.finxter.com\/python-complex\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python complex() &#8212; A Useless Python Feature?<\/a> first appeared on <a href=\"https:\/\/blog.finxter.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Finxter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Python complex() method returns a complex number object. You can either pass a string argument to convert the string to a complex number, or you provide the real and imaginary parts to create a new complex number from those. This article shows you how to use Python&#8217;s built-in complex() constructor. You&#8217;ll not only learn [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-122231","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/122231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=122231"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/122231\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=122231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=122231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=122231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}