Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python complex() — A Useless Python Feature?

#1
Python complex() — A Useless Python Feature?

<div><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>
<p>This article shows you how to use Python’s built-in <code>complex()</code> constructor. You’ll not only learn how to use it—but also <strong><em>why it is useless</em></strong> and what you should do instead in newer Python versions. </p>
<h2>Usage</h2>
<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>
<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)
(1-2j)
>>> complex(2, -1)
(2-1j)
>>> complex(2, 2)
(2+2j)
>>> complex(1)
(1+0j)
>>> complex(2)
(2+0j)
>>> complex('42-21j')
(42-21j)</pre>
</p>
<h2>Syntax Complex()</h2>
<p>You can use the <code>complex()</code> method with three different argument lists. </p>
<pre class="wp-block-preformatted"><strong>Syntax: </strong>
<code><strong>complex(real)</strong> # Imaginary Part is 0j</code>
<code><strong>complex(real, img)</strong> # Both real and imaginary part are given</code>
<code><strong>complex(string) </strong> # String has format 'x+yj' for real part x and imaginary part y. </code></pre>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><strong>Arguments</strong></td>
<td><code>real</code></td>
<td>The real part of the complex number</td>
</tr>
<tr>
<td></td>
<td><code>img</code></td>
<td>The imaginary part of the complex number</td>
</tr>
<tr>
<td></td>
<td><code>string</code></td>
<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>
</tr>
<tr>
<td><strong>Return Value</strong></td>
<td><code>complex</code></td>
<td>Returns a complex number.</td>
</tr>
</tbody>
</table>
</figure>
<h2>Video Complex()</h2>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe 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>
</div>
</figure>
<h2>Interactive Shell Exercise: Understanding Complex()</h2>
<p>Consider the following interactive code:</p>
<p> <iframe src="https://trinket.io/embed/python/fdfd3b9a88" marginwidth="0" marginheight="0" allowfullscreen="" width="100%" height="356" frameborder="0"></iframe> </p>
<p><em><strong>Exercise</strong>: </em>Guess the output before running the code. </p>
<hr class="wp-block-separator"/>
<p><strong>But before we move on, I’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>
<p>If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a <strong>single line of Python code.</strong> But it’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>
<div class="wp-block-image">
<figure class="aligncenter"><a href="https://amzn.to/2WAYeJE" target="_blank" rel="noopener noreferrer"><img 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>
</div>
<p>The book is released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco). </p>
<p>Link: <a href="https://nostarch.com/pythononeliners" target="_blank" rel="noreferrer noopener">https://nostarch.com/pythononeliners</a></p>
<hr class="wp-block-separator"/>
<h2>How to Create Complex Number Without complex()</h2>
<p>Interestingly, you don’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—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>
<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
b = 4+42j
c = 0+0j print('Complex Numbers:')
print(a, b, c) print('Types:')
print(type(a), type(b), type©)
</pre>
<p>The output is:</p>
<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:
(1+1j) (4+42j) 0j
Types:
&lt;class 'complex'> &lt;class 'complex'> &lt;class 'complex'></pre>
</p>
<h2>Summary</h2>
<p>The Python <code>complex()</code> method returns a complex number object. To create a complex number:</p>
<ul>
<li>Pass a string argument to convert the string to a complex number, or </li>
<li>Provide the real and imaginary parts to create a new complex number from those.</li>
</ul>
<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>
<hr class="wp-block-separator"/>
<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>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<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>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</p>
</p>
<p>The post <a href="https://blog.finxter.com/python-complex/" target="_blank" rel="noopener noreferrer">Python complex() — A Useless Python Feature?</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


https://www.sickgaming.net/blog/2020/12/...n-feature/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016