<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adrian Perea</title>
    <description>The latest articles on DEV Community by Adrian Perea (@adrianmarkperea).</description>
    <link>https://dev.to/adrianmarkperea</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F387820%2Fca7abc57-21d8-4b0a-bef2-d153a13b919f.png</url>
      <title>DEV Community: Adrian Perea</title>
      <link>https://dev.to/adrianmarkperea</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adrianmarkperea"/>
    <language>en</language>
    <item>
      <title>Better Python String Formatting Using f-strings</title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Sun, 13 Dec 2020 03:24:54 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/better-python-string-formatting-using-f-strings-1kn3</link>
      <guid>https://dev.to/adrianmarkperea/better-python-string-formatting-using-f-strings-1kn3</guid>
      <description>&lt;p&gt;As an avid Python user, I've always been jealous of how JavaScript users can &lt;em&gt;seamlessly&lt;/em&gt; put together strings and expressions using template literals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Fifteen is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;
&lt;span class="nx"&gt;Fifteen&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is an extremely elegant solution, as opposed to the de facto standard of using Python string formatting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Fifteen is {}.'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Fifteen&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="mf"&gt;15.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might say, "But, Adrian! The Python version doesn't seem so bad." And you're correct... when we're talking about using single expressions. But what if we're using more than one? &lt;/p&gt;

&lt;p&gt;Skim through the following code examples in both JavaScript and Python to see how quickly things can get tricky.&lt;/p&gt;

&lt;p&gt;Here's the JavaScript version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Adrian&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;major&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Computer Engineering&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;occupation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Software Engineer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello! My name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;. 
                I am &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old.
                I studied &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;major&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; in college,
                and I am currently working as a &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;occupation&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;My&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;Adrian&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;am&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="nx"&gt;years&lt;/span&gt; &lt;span class="nx"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;studied&lt;/span&gt; &lt;span class="nx"&gt;Computer&lt;/span&gt; &lt;span class="nx"&gt;Engineering&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;college&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;am&lt;/span&gt; &lt;span class="nx"&gt;currently&lt;/span&gt; &lt;span class="nx"&gt;working&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;Software&lt;/span&gt; &lt;span class="nx"&gt;Engineer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the Python version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'Adrian'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;major&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'Computer Engineer'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;occupation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'Software Engineer'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'''Hello! My name is {}.
             I am {} years old.
             I studied {} in college,
             and I am currently working as a {}.
          '''&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;major&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;occupation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Hello&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt; &lt;span class="n"&gt;My&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;Adrian&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;studied&lt;/span&gt; &lt;span class="n"&gt;Computer&lt;/span&gt; &lt;span class="n"&gt;Engineering&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;college&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="n"&gt;currently&lt;/span&gt; &lt;span class="n"&gt;working&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;Software&lt;/span&gt; &lt;span class="n"&gt;Engineer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem with the Python version is that it requires you to have a mental mapping between the expressions you need to include in the string and the supplying the expressions at the end of the string. It's easy to mess up the arrangement, especially when you're working on more complex code.&lt;/p&gt;

&lt;p&gt;Compare this with the JavaScript version that has the expressions in-line. There's less room for errors.&lt;/p&gt;

&lt;p&gt;Python has a solution for this, and it's naming the string formatting braces. This allows you to supply the expressions in the &lt;code&gt;format&lt;/code&gt; method as keyword arguments, depending on where on the string you want it to go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'''Hello! My name is {name}.
         I am {age} years old.
         I studied {major} in college,
         and I am currently working as a {occupation}.
      '''&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;major&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;major&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;occupation&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;occupation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Hello&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt; &lt;span class="n"&gt;My&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;Adrian&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;studied&lt;/span&gt; &lt;span class="n"&gt;Computer&lt;/span&gt; &lt;span class="n"&gt;Engineering&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;college&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="n"&gt;currently&lt;/span&gt; &lt;span class="n"&gt;working&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;Software&lt;/span&gt; &lt;span class="n"&gt;Engineer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this solves the problem of mental mapping, it can get quite verbose. It's not exactly the best solution.&lt;/p&gt;

&lt;p&gt;So what are our options? Fortunately, the smart people at Python have introduced &lt;strong&gt;f-strings&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What the f-strings
&lt;/h2&gt;

&lt;p&gt;F-strings are nominally called &lt;a href="https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings"&gt;Formatted String Literals&lt;/a&gt;. They're usually called f-strings since you construct them by adding an &lt;code&gt;f&lt;/code&gt; before a string, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;f'I am a formatted string!'&lt;/span&gt;
&lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;formatted&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what can we do with f-strings? Actually, its usage is very similar to the string formatting in JavaScript examples we saw above. Let's see the first example rewritten using f-strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f'Fifteen is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;Fifteen&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="mf"&gt;15.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nice! That already looks so much cleaner. Now let's rewrite the second example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'Adrian'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;major&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'Computer Engineer'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;occupation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'Software Engineer'&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f'''Hello! My name is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.
             I am &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; years old.
             I studied &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;major&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; in college,
             and I am currently working as a &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;occupation&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.
          '''&lt;/span&gt;
&lt;span class="n"&gt;Hello&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt; &lt;span class="n"&gt;My&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;Adrian&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;studied&lt;/span&gt; &lt;span class="n"&gt;Computer&lt;/span&gt; &lt;span class="n"&gt;Engineering&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;college&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="n"&gt;currently&lt;/span&gt; &lt;span class="n"&gt;working&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;Software&lt;/span&gt; &lt;span class="n"&gt;Engineer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, using f-strings allow us to &lt;em&gt;string&lt;/em&gt; together literals and expressions seamlessly, just like in JavaScript! But since I am a &lt;strong&gt;hardcore fan&lt;/strong&gt; of Python, I want to show you additional things you can do with f-strings that you &lt;em&gt;can't&lt;/em&gt; do with JavaScript string formatting:&lt;/p&gt;

&lt;h3&gt;
  
  
  Rounding Off Digits
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;math&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f'The value of pi is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="mf"&gt;3.141592653589793&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f'The value of pi is approximately &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;:.&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;approximately&lt;/span&gt; &lt;span class="mf"&gt;3.142&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Aligning Items
&lt;/h3&gt;

&lt;p&gt;This ensures that &lt;code&gt;name&lt;/code&gt; is at least 10 characters wide, and &lt;code&gt;quantity&lt;/code&gt; is 4 characters wide.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;shopping_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'Bananas'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Apples'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Grapes'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;shopping_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; : &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;Bananas&lt;/span&gt;   &lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="mi"&gt;2&lt;/span&gt;  
&lt;span class="n"&gt;Apples&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="mi"&gt;4&lt;/span&gt;  
&lt;span class="n"&gt;Grapes&lt;/span&gt;    &lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  String formatting is not fun
&lt;/h2&gt;

&lt;p&gt;I think many of us can agree that string formatting is not fun, so we may as well use methods that make it easier not only to us, but also the readers of our code. Using Python f-strings allows us to write cleaner, more concise, intuitive, and especially, more readable code by seamlessly embedding expressions within string literals.&lt;/p&gt;

&lt;p&gt;The examples I showed above are only the tip of the iceberg. You can find out more examples and inspiration for string formatting through the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;a href="https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings"&gt;official documentation&lt;/a&gt; on f-strings&lt;/li&gt;
&lt;li&gt;The &lt;a href="https://docs.python.org/3/library/string.html#formatspec"&gt;format specification mini-language&lt;/a&gt; to learn more about how to modify expressions within the strings&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Iterate Through Multiple Python Lists at Once</title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Mon, 23 Nov 2020 01:18:36 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/how-to-iterate-through-multiple-python-lists-at-once-3a9d</link>
      <guid>https://dev.to/adrianmarkperea/how-to-iterate-through-multiple-python-lists-at-once-3a9d</guid>
      <description>&lt;p&gt;The Python &lt;a href="https://docs.python.org/3.3/library/functions.html#zip"&gt;zip&lt;/a&gt; built-in function makes it easy to iterate through multiple iterables all at once. It aggregates elements from each iterable to create an iterator of tuples. The &lt;em&gt;i-th&lt;/em&gt; tuple contains the &lt;em&gt;i-th&lt;/em&gt; element of each of the constituent iterables. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;zipped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;zipped&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; 
&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Getting the zipped elements as a list
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;zip&lt;/code&gt; function returns a -- wait for it -- &lt;code&gt;zip&lt;/code&gt; object. You can confirm this by printing the &lt;code&gt;zipped&lt;/code&gt; variable out in the console. You should see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;zipped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;zip&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;ox7f45ddba2c08&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To convert it into a &lt;code&gt;list&lt;/code&gt; you can manipulate, you use the very aptly named &lt;code&gt;list&lt;/code&gt; function like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;zipped&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Unzipping a zip object
&lt;/h2&gt;

&lt;p&gt;If for any reason you need to reverse a &lt;code&gt;zip&lt;/code&gt; operation, you can do so with the &lt;code&gt;zip&lt;/code&gt; function, in conjunction with the destructuring operator (&lt;code&gt;*&lt;/code&gt;) to return the original itertables as tuples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nb"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="bp"&gt;True&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Zipping unequal list lengths
&lt;/h2&gt;

&lt;p&gt;Zipping unequal list lengths causes the elements of the longer lists to be dropped:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fortunately, we can use the &lt;a href="https://docs.python.org/3.3/library/itertools.html#itertools.zip_longest"&gt;itertools.zip_longest&lt;/a&gt; function to solve this. &lt;code&gt;zip_longest&lt;/code&gt; takes iterables as paramaters like &lt;code&gt;zip&lt;/code&gt;, but with an additional &lt;code&gt;fillvalue&lt;/code&gt; parameter which substitutes for missing values. It defaults to &lt;code&gt;None&lt;/code&gt;. Here's how you use it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;itertools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;zip_longest&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;zip_longest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with a &lt;code&gt;fillvalue&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;zip_longest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fillvalue&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, with Python, it's very simple to iterate over multiple lists at once. &lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The official documentation for &lt;a href="https://docs.python.org/3.3/library/functions.html#zip"&gt;zip&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The official documentation for &lt;a href="https://docs.python.org/3.3/library/itertools.html#itertools.zip_longest"&gt;itertools&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;How to use the &lt;a href="https://www.adrianperea.dev/better-python-for-loops-using-enumerate/"&gt;enumerate&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Better Python for Loops Using enumerate</title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Thu, 19 Nov 2020 01:52:10 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/better-python-for-loops-using-enumerate-4630</link>
      <guid>https://dev.to/adrianmarkperea/better-python-for-loops-using-enumerate-4630</guid>
      <description>&lt;p&gt;It's a common use case to iterate over a list together with its index. Traditionally, this has been done by using a &lt;code&gt;for&lt;/code&gt; loop, utilizing &lt;code&gt;range&lt;/code&gt; and &lt;code&gt;len&lt;/code&gt; to create a list of indices. The items of the list then are accessed using &lt;code&gt;[]&lt;/code&gt; notation, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;shopping_cart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'apple'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'cereal'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'banana'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'cola'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shopping_cart&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;shopping_cart&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 0 apple
# 1 cereal
# 2 banana
# 3 cola
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this solution is very imperative, reminiscent of older programming languages. We have a better, more pythonic solution.&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;enumerate&lt;/code&gt; keyword, we can get an iterator that already gives both the item and index of the list. Using this, we can simplify the above code as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;shopping_cart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'apple'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'cereal'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'banana'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'cola'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shopping_cart&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 0 apple
# 1 cereal
# 2 banana
# 3 cola
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I personally think that this solution is cleaner, more elegant, and simpler to write. &lt;/p&gt;




&lt;p&gt;&lt;em&gt;Hey! My name is Adrian. I'm a software engineer who likes to write. If you liked this, why not &lt;a href="https://twitter.com/adrianmarkperea"&gt;follow&lt;/a&gt; me on Twitter&lt;/em&gt;?&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Learn Docker by Dockerizing a Flask Application</title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Fri, 23 Oct 2020 01:16:34 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/learn-docker-by-dockerizing-a-flask-application-4epg</link>
      <guid>https://dev.to/adrianmarkperea/learn-docker-by-dockerizing-a-flask-application-4epg</guid>
      <description>&lt;p&gt;The value proposition of &lt;strong&gt;Docker Containers&lt;/strong&gt; is that they allow you to create applications that run practically everywhere. While this makes it sound like it's a nightmare to create, it's actually pretty simple!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.adrianperea.dev%2Fstatic%2F298d706eb569215231ff9ae736b83e94%2Fe996b%2Fdocker.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.adrianperea.dev%2Fstatic%2F298d706eb569215231ff9ae736b83e94%2Fe996b%2Fdocker.png" alt="Docker"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to demonstrate this, we'll attempt to &lt;em&gt;dockerize&lt;/em&gt; (the act of transforming a standalone application to a container-compatible one) a simple Flask application. By the end of this article, you'll be up and running with Docker containers!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;All the files can be found in this &lt;a href="https://github.com/adrianmarkperea/docker-flask-demo" rel="noopener noreferrer"&gt;repo&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's first create a folder for our project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;docker-flask-demo &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;docker-flask-demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, let's create our simple Flask application and name it &lt;code&gt;app.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# docker-flask-demo/app.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;flask&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Flask&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Flask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello_world&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello world, from your container!&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;0.0.0.0&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might be thinking that we need to install Flask at this point. But instead of installing Flask (and any other dependencies) in our own local system, &lt;strong&gt;we'll instruct Docker to install these dependencies inside the container&lt;/strong&gt;. By doing this, our containers can run independently in any environment. Neat, right?&lt;/p&gt;

&lt;p&gt;One thing that we have to do is to tell our container what dependencies are needed. Since we're using Python, what we need is a &lt;code&gt;requirements.txt&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;Let's create this file and tell our container that we need the Flask module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# docker-flask-demo/requirements.txt
&lt;/span&gt;
&lt;span class="n"&gt;flask&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last thing that we need is a &lt;code&gt;Dockerfile&lt;/code&gt;. The Dockerfile is responsible for  specifying our &lt;a href="https://docs.docker.com/engine/reference/commandline/images/" rel="noopener noreferrer"&gt;Docker Image&lt;/a&gt;, which we'll then use as a blueprint for our containers. Think of it as a container prototype.&lt;/p&gt;

&lt;p&gt;Create the &lt;code&gt;Dockerfile&lt;/code&gt; (no extensions) in the project folder and write the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; ubuntu:18.04&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;apt-get update &lt;span class="nt"&gt;-y&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; python-pip python-dev

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . /app&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt

&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; [ "python" ]&lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; [ "app.py" ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's look at the different parts of this file one by one:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;FROM&lt;/strong&gt;: Specifies the base image to be used for all the commands. This allows us to reuse existing Docker images and add our own rules. In this case, we're using with a clean Ubuntu image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RUN&lt;/strong&gt;: Allows us to run shell commands. We're using this to update our system and install system dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;COPY&lt;/strong&gt;: Copies files from our local system to the container file system. Here we're copying our local &lt;code&gt;requirements.txt&lt;/code&gt; to the container's &lt;code&gt;/app/&lt;/code&gt; folder.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WORKDIR&lt;/strong&gt;: Changes the current working directory in the container. All the subsequent commands after this command will be ran in this directory. Here we're moving to the &lt;code&gt;/app/&lt;/code&gt; folder.&lt;/li&gt;
&lt;li&gt;We then use &lt;code&gt;RUN&lt;/code&gt; to install our application requirements using &lt;code&gt;pip&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ENTRYPOINT&lt;/strong&gt;: Determines the executable that will be invoked when we run our container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CMD&lt;/strong&gt;: Specifies default arguments to our executable. This means that by default our container will run our flask application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's now build this image! Run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; docker-flask-demo:latest &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command tells docker to fetch the &lt;code&gt;Dockerfile&lt;/code&gt; present in the current directory (hence the &lt;code&gt;.&lt;/code&gt; at the end). Docker then uses this file to create an image and names it &lt;code&gt;docker-flask-demo&lt;/code&gt; with the tag &lt;code&gt;latest&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We won't delve much into tags for now, but think of them as ways to specify the version of your image. In this case, we're saying that this build is the latest verion of our image.&lt;/p&gt;

&lt;p&gt;After the command runs, we can see that our image is now installed using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;docker image &lt;span class="nb"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would give a result similar to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker-flask-demo   latest              676bb635f471        18 seconds ago      446MB
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can finally use this image to create our own containers! A simple way to do this is through the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; 5000:5000 docker-flask-demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running this command, our container should be up and running our application! Go open your browser and go to &lt;code&gt;localhost:5000&lt;/code&gt;. You should see the following page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.adrianperea.dev%2Fstatic%2Feb9fd95579d6e2c2732903682b2eed7d%2Fb4708%2Frunning.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.adrianperea.dev%2Fstatic%2Feb9fd95579d6e2c2732903682b2eed7d%2Fb4708%2Frunning.jpg" alt="Running in a container"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;docker run&lt;/code&gt; command that we just ran creates and runs a container based on the &lt;code&gt;docker-flask-demo&lt;/code&gt; image we created. The &lt;code&gt;-d&lt;/code&gt; option allows us to run the container in the background, and the &lt;code&gt;-p&lt;/code&gt; command maps the port 5000 of our local system with the port 5000 of the container. We add this command because by default, the ports of the container are not accessible by our local system.&lt;/p&gt;

&lt;p&gt;We can see all our running containers by issuing the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker ps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which should give the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
d95eb8940835        docker-flask-demo   &lt;span class="s2"&gt;"python app.py"&lt;/span&gt;     4 minutes ago       Up 4 minutes        0.0.0.0:5000-&amp;gt;5000/tcp   keen_jackson
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally, we can stop our container (and consequently, our application) by issuing the following command together with the &lt;code&gt;CONTAINER ID&lt;/code&gt; of our container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker stop d95eb8940835
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it! We successfully dockerized our Flask application and made it run in a container. We installed all the dependencies inside the container, so we can be sure that this application will run in any environment.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Reinvent the Wheel, then Display It Like a Trophy</title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Thu, 16 Jul 2020 08:55:37 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/reinvent-the-wheel-then-display-it-like-a-trophy-5bk</link>
      <guid>https://dev.to/adrianmarkperea/reinvent-the-wheel-then-display-it-like-a-trophy-5bk</guid>
      <description>&lt;p&gt;People talk about "reinventing the wheel" as if it's some sort of curse. It's true: in this day and age, tons of libraries are already available for your picking. These libraries have a &lt;em&gt;lot&lt;/em&gt; of people working on it, so you can be assured that all the bases have already been covered (as much as possible). &lt;/p&gt;

&lt;p&gt;Why recreate something that's already been created? And more: why recreate something that has a very likely to be a &lt;em&gt;worse&lt;/em&gt; version of what's already there?&lt;/p&gt;

&lt;p&gt;If we were talking about utility, then the recreation has no point. But from the point of view of the craftsman, it means the world.&lt;/p&gt;

&lt;h1&gt;
  
  
  Caring for our Craft
&lt;/h1&gt;

&lt;p&gt;Engineering has always been associated with logic. We work daily with equations, syntax, optimizations, and all of those nuts and bolts to make sure that our products are working as smoothly and as fast as they can. We decide -- through logic -- everything: from architecture all the way to the last semicolon.&lt;/p&gt;

&lt;p&gt;But at the end of the day, it's our innovation, creativity, and ingenuity that drives all of these decisions. We won't have a project without an idea. &lt;/p&gt;

&lt;p&gt;The paradoxical truth is that engineering is art more often than it is engineering.&lt;/p&gt;

&lt;p&gt;And for any craft that works with art, care is a must. &lt;/p&gt;

&lt;p&gt;A baker has to hand-pick ingredients, knead the dough properly, measure accurately, and understand the nuance of every decision in order to create the perfect batch of bread. Truly, bread created this way will be said to have been made with care.&lt;/p&gt;

&lt;p&gt;On the other hand, if the baker uses stale ingredients, kneads the dough hastily, measure inaccurately, and just perform random decisions (possibly due to inexperience), the result would be stale and lifeless bread.&lt;/p&gt;

&lt;p&gt;Only by creating, making mistakes, destroying, and once again recreating, will a baker be able to create bread with heart. That is, bread that's been cared for.&lt;/p&gt;

&lt;h1&gt;
  
  
  Reinvent the Wheel
&lt;/h1&gt;

&lt;p&gt;Just like the baker in our story, us engineers would get by just fine by using bottled up solutions without truly understanding the nuances behind them. Our customers would most likely be none the wiser.&lt;/p&gt;

&lt;p&gt;No. Reinventing the wheel is not for them.&lt;/p&gt;

&lt;p&gt;Reinventing the wheel is for us. It's for the author of the code. It's for the readers of the code. It's for other engineers who want to master the craft, and learn how to be master craftsmen in the field.&lt;/p&gt;

&lt;p&gt;Reinventing the wheel allows us to learn from what other engineers have experienced, and then make their experiences our own. It allows us to understand every decision, why a function was broken down, and why a variable was named as it was. &lt;/p&gt;

&lt;p&gt;Reinventing the wheel is the most efficient way for engineers to assimilate knowledge from those who've already walked our paths.&lt;/p&gt;

&lt;p&gt;We do this not for any clout, or to please anyone else. We do this for the love of the craft. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YSFqoc9s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/960/1%2ACdFtk1VHomAiao1HcFBwgg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YSFqoc9s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/960/1%2ACdFtk1VHomAiao1HcFBwgg.png" alt="Reinvent the wheel"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So reinvent the wheel, and display it as a trophy. Be proud of the art you've created.&lt;/p&gt;

</description>
      <category>beginners</category>
    </item>
    <item>
      <title>A Gentle Introduction to Genetic Algorithms</title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Mon, 13 Jul 2020 15:06:57 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/a-gentle-introduction-to-genetic-algorithms-38l1</link>
      <guid>https://dev.to/adrianmarkperea/a-gentle-introduction-to-genetic-algorithms-38l1</guid>
      <description>&lt;p&gt;The &lt;strong&gt;genetic algorithm&lt;/strong&gt; is a search heuristic inspired by Darwin's theory of evolution.  This algorithm borrows the following concepts from natural selection:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each individual (solution) has an associated fitness score&lt;/li&gt;
&lt;li&gt;Individuals with high fitness scores are selected for reproduction&lt;/li&gt;
&lt;li&gt;Chosen individuals reproduce to create offspring with the characteristics of both parents&lt;/li&gt;
&lt;li&gt;Some offspring would have random mutations applied to them&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The idea is that if the parents have high fitness, the offspring would have high fitness as well.&lt;/p&gt;

&lt;p&gt;This entire process of &lt;strong&gt;selection&lt;/strong&gt;, &lt;strong&gt;reproduction&lt;/strong&gt; (more commonly known as crossover), and &lt;strong&gt;mutation&lt;/strong&gt; will repeat many times. At the end, only the fittest individuals will remain. These fittest individuals represent the solutions to our problem.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Only the fittest will survive - Charles Darwin&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before diving deeper, let's first understand what genetic algorithms are trying to solve.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Infinite Monkey Theorem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X7gJ02C_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/static/806170d93160e3d53000fef7b80d7818/acb04/infinite-monkey-theorem.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X7gJ02C_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/static/806170d93160e3d53000fef7b80d7818/acb04/infinite-monkey-theorem.jpg" alt="The Ifninite Monkey Theorem"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The infinite monkey theorem pictures a monkey hitting keys at random on a typewriter.&lt;/p&gt;

&lt;p&gt;It suggests that if the monkey hits on the keyboard for an infinite amount of time, it will be able to type any given text. Yes, even the works of William Shakespeare.&lt;/p&gt;

&lt;p&gt;Although, the probability of doing so is extremely low.&lt;/p&gt;

&lt;p&gt;Let's see an example.&lt;/p&gt;

&lt;p&gt;Imagine that the monkey has a primitive keyboard with only 27 characters. These are the small letters a to z and the space character.&lt;/p&gt;

&lt;p&gt;The monkey's task is to write the phrase: if music be the food of love play on.&lt;/p&gt;

&lt;p&gt;How likely will the monkey write this phrase?&lt;/p&gt;

&lt;p&gt;To write "i": 1/27&lt;/p&gt;

&lt;p&gt;To write "if": 1/27 * 1/27&lt;/p&gt;

&lt;p&gt;To write the entire phrase (36 characters, including spaces): (1/27)^36&lt;/p&gt;

&lt;p&gt;In other words, the probability of the monkey typing this phrase in random is:&lt;/p&gt;

&lt;p&gt;1 in  3,381,391,910,000,000,000,000,000,000,000,000,000,000,000,000,000,000.&lt;/p&gt;

&lt;p&gt;And even if the monkey were able to write one million phrases per second, to be able to write this phrase randomly at least &lt;em&gt;once&lt;/em&gt;, it would take this much time:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5,142,335,400,000,000,000,000,000,000,000,000,000,000,000,000 years&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(As a comparison, the universe is only 13,800,000,000 years old)&lt;/p&gt;

&lt;p&gt;Nobody has this kind of time. So, how can we improve this?&lt;/p&gt;

&lt;h2&gt;
  
  
  The Power of Genetic Algorithms
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PduGu8Sh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/4908ee84c935a096b70a9070055f49d7/anatomy-of-genetic-algorithms.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PduGu8Sh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/4908ee84c935a096b70a9070055f49d7/anatomy-of-genetic-algorithms.svg" alt="Anatomy of Genetic Algorithms"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The answer is through genetic algorithms. Genetic algorithms arrive at a solution magnitudes faster than brute force search. To do so, it uses the following two ideas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some solutions are better (or more fit) than others&lt;/li&gt;
&lt;li&gt;We combine fit solutions with each other to get a high chance of getting fitter solutions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll discuss these two ideas more shortly, but for now, keep them in mind. Genetic algorithms make use of these ideas in 6 different steps:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6Li2M3zd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/d10a072d72e8f2b894bbcd681e359555/genetic-algorithms-flow-chart.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6Li2M3zd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/d10a072d72e8f2b894bbcd681e359555/genetic-algorithms-flow-chart.svg" alt="Genetic Algorithms Flow Chart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's discuss them one by one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Initial Population
&lt;/h2&gt;

&lt;p&gt;Genetic algorithms begin by defining a set of individuals called a population. Each of these individuals is a potential solution to the problem you want to solve.&lt;/p&gt;

&lt;p&gt;Each individual is defined by a set of parameters called genes. These genes are joined together to define an individual's chromosome. It's the individuals' chromosomes that define our solution.&lt;/p&gt;

&lt;p&gt;But what should these parameters be?&lt;/p&gt;

&lt;p&gt;It depends on the problem, but generally, the set of genes comes from a pre-defined alphabet. Usually, this alphabet is the binary alphabet: each gene can have a value of 1 or 0 (chosen at random).&lt;/p&gt;

&lt;p&gt;In our case, we want each of our individual (solution) to be a guess of the phrase: if music be the food of love play on. To create solutions for this, we need a much more sophisticated alphabet than 1s and 0s.&lt;/p&gt;

&lt;p&gt;What we can do is define our alphabet to be 27 characters: small letters a to z and the spacebar. Each individual defines itself by a string of small letter characters (and the spacebar). &lt;/p&gt;

&lt;p&gt;For example, if we were trying to solve for the word apple, our solutions would look like the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4ultbJhO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/cb1e999d17cfa70aff17a43be830ac64/initial-population.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4ultbJhO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/cb1e999d17cfa70aff17a43be830ac64/initial-population.svg" alt="Initial Population"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are all randomized strings that are 5 characters long. As you can see, the solutions can range from being completely off the mark to being close to the target. There’s even a possibility to get the solution on the first try!&lt;/p&gt;

&lt;p&gt;What we want to do is to tell our algorithm that we should choose more solutions that are closer to our target. By eliminating bad solutions and choosing the ones that are closer to our target, we can converge to the right solution faster.&lt;/p&gt;

&lt;p&gt;But how can we tell how good (fit) each solution is? That’s what the fitness function is for.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fitness Function
&lt;/h2&gt;

&lt;p&gt;The fitness function determines how "fit" or how good a solution an individual is. The fitness function assigns a fitness score to an individual based on its genes.&lt;/p&gt;

&lt;p&gt;Individuals with a higher score are more likely to be chosen at random for the next generation of individuals. By selecting individuals close to our solution, we can ignore all the other solutions that are off the mark.&lt;/p&gt;

&lt;p&gt;You can define the fitness function any and which way that you want. That's what makes it powerful. It can be flexible enough to accommodate any problem.&lt;/p&gt;

&lt;p&gt;In our case, we can define it as "the number of matching characters in our individual and the target phrase." So for example, if we were trying to guess the phrase apple, these individuals will be scored as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wjA90Y97--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/cbca68027c1a98b15b82205a242b8306/fitness-function.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wjA90Y97--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/cbca68027c1a98b15b82205a242b8306/fitness-function.svg" alt="Fitness Function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the second and third solutions are closer to our target, we want to prioritize those two over the first solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selection
&lt;/h2&gt;

&lt;p&gt;The selection phase lets us select the fittest individuals and allow them to pass their genes to the next generation. The individuals chosen in this phase are called the parents.&lt;/p&gt;

&lt;p&gt;There are many ways to select the parents. One of the most common ways is called Roulette Wheel Selection or Fitness Proportionate Selection. As the name suggests, the probability of choosing an individual is proportionate to its fitness score.&lt;/p&gt;

&lt;h2&gt;
  
  
  Crossover
&lt;/h2&gt;

&lt;p&gt;Crossover is the workhorse of genetic algorithms. It allows the parents (chosen from the selection phase) to exchange their genes.&lt;/p&gt;

&lt;p&gt;The idea is that if we exchange the genes of two fit solutions, we'll arrive at a solution that's fitter.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8gxWm3Lp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/caa9b476b4705e05e6a6666729a7bb56/crossover.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8gxWm3Lp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.adrianperea.dev/caa9b476b4705e05e6a6666729a7bb56/crossover.svg" alt="Crossover"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Like selection, there are many different ways we can perform crossover. The simplest is Single Point Crossover. For each pair of parents we chose during selection, we create new individuals (offspring) by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choosing a random crossover point from the genes&lt;/li&gt;
&lt;li&gt;Selecting genes from the first parent until we reach the crossover point&lt;/li&gt;
&lt;li&gt;Selecting genes from the second parent until the end of the string&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These offspring are then added to the new population.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mutation
&lt;/h2&gt;

&lt;p&gt;When we create our initial population, we also define the genes that are present in the population. During crossover, these genes are exchanged amongst each individual to arrive at our solution.&lt;/p&gt;

&lt;p&gt;But what if we never get genes that are required for the solution? Going back to the apple example, what if our individuals, never randomly generate the letter a? Crossover only exchanges existing genes. Even if we perform crossover till the end of time, we will never find our solution.&lt;/p&gt;

&lt;p&gt;To resolve this, we perform mutation. Mutation is as the name suggest. We subject a gene to mutate (in our case, to a different random letter) with a low random probability.&lt;/p&gt;

&lt;p&gt;This allows us to maintain diversity in our population and ensure that we arrive at a solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stop Condition
&lt;/h2&gt;

&lt;p&gt;The algorithm repeats the loop until the population has either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Converged (the generated offspring isn't much different from the previous generation)&lt;/li&gt;
&lt;li&gt;Reached a certain number of generations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the algorithm stops, we can say that the final set of individuals is the solution to our problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solving the Infinite Monkey Theorem
&lt;/h2&gt;

&lt;p&gt;Let’s see how we can apply what we learned to solve the infinite monkey theorem.&lt;br&gt;
As a refresher, we are trying to solve for the phrase if music be the food of love play on. &lt;/p&gt;

&lt;p&gt;This phrase has 36 characters, so each individual will have a chromosome with 36 genes. Each gene will start with a randomized lowercase letter (or space). A chromosome represents a potential solution of the phrase.&lt;/p&gt;

&lt;p&gt;The fitness value of each chromosome is calculated based on how many characters it got correct in the proper position. A chromosome that guesses the phrase exactly has a perfect fitness score of 36. A chromosome with no correct characters, on the other hand, has a fitness score of 0.&lt;/p&gt;

&lt;p&gt;The aim of our genetic algorithm is to maximize the fitness function. So, individuals with higher fitnesses are selected over individuals with low fitnesses. At the end, we expect our population to have an individual with the perfect score of 36.&lt;/p&gt;

&lt;p&gt;By doing this, we can achieve the following result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aJdZjLms--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://www.adrianperea.dev/c78f5ca731b2a8243f2bdd353a31ce52/genetic-algorithms-demo.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aJdZjLms--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://www.adrianperea.dev/c78f5ca731b2a8243f2bdd353a31ce52/genetic-algorithms-demo.gif" alt="Genetic Algorithms Demo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are some few comments about it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For each iteration, the algorithm loops through the process we described above until either A) it reaches the maximum of 10,000 generations, or B) it reaches our target phrase.&lt;/li&gt;
&lt;li&gt;Out of the total population of 5000, the top 50 solutions are displayed.&lt;/li&gt;
&lt;li&gt;The right number shows the fitness, i.e. the number of correct genes in the solution.&lt;/li&gt;
&lt;li&gt;Incorrect genes have a red background.&lt;/li&gt;
&lt;li&gt;The solutions are ordered according to fitness, with the solution with the highest fitness on top and the one with the lowest fitness at the bottom.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next part of this series, we’ll walk step by step on how to code this. For the curious, here’s where you can find the &lt;a href="https://stoic-hoover-d22e34.netlify.app/"&gt;final result&lt;/a&gt; and the &lt;a href="https://github.com/adrianmarkperea/introduction-to-genetic-algorithms"&gt;source code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: since genetic algorithms are relatively resource intensive, the demo might be slow on mobile.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Genetic algorithms are algorithms inspired by Darwin’s theory of evolution. In a nutshell, it uses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Natural selection to select the best solutions to a problem &lt;/li&gt;
&lt;li&gt;Crossover mix the best solutions to create even better solutions&lt;/li&gt;
&lt;li&gt;Mutation in order to maintain diversity in the population&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Through these, best solutions are kept and bad solutions are removed quickly. This allows us to arrive at the ideal solution for a search problem relatively ease.&lt;/p&gt;

&lt;p&gt;Genetic algorithms are a prime example of how much we can learn from nature. I hope that through this article (and the rest that will come from this series), you will find a new appreciation for how sophisticated and beautiful our natural world is.&lt;/p&gt;

&lt;p&gt;See you around next time!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>What is __name__ in Python? </title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Mon, 29 Jun 2020 14:13:30 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/what-is-name-in-python-43i2</link>
      <guid>https://dev.to/adrianmarkperea/what-is-name-in-python-43i2</guid>
      <description>&lt;p&gt;You’ve most likely seen the following piece of code at the bottom of a Python script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="n"&gt;__main__&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You've most likely seen the following piece of code at the bottom of a script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="c1"&gt;# some code here
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;While this line of code may seem mundane, it's actually quite powerful! It allows you to run python code as standalone scripts or import them as modules.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;__name__&lt;/code&gt; variable is a special "dunder" or magic variables in Python. Dunder stands for "double underscore", hence the two underscores on both sides.&lt;/p&gt;

&lt;p&gt;Dunder variables hold special meaning in Python. The value of the &lt;code&gt;__name__&lt;/code&gt; dunder variable depends on how you execute the script.&lt;/p&gt;

&lt;p&gt;If you run your script from the command line, the &lt;code&gt;__name__&lt;/code&gt; has the value &lt;code&gt;__main__&lt;/code&gt;.  If you import it, it will contain the name of the script.&lt;/p&gt;

&lt;p&gt;Let's see how this works through code.&lt;/p&gt;

&lt;h2&gt;
  
  
  A simple example
&lt;/h2&gt;

&lt;p&gt;Suppose you're making a simple greeter module. You want it to behave as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When it's run as a standalone script, it asks the name of the user and prints "Hi, {user}!". It repeats this until the program exits.&lt;/li&gt;
&lt;li&gt;When it's imported, it exposes a &lt;code&gt;greet&lt;/code&gt; function that can create a greeting given a name&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Seems simple enough! Let's code it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# greeter.py
&lt;/span&gt;
&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
   &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f'Hi, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;!'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'__name__ is:'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What's your name?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'&amp;gt; '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'q'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;

      &lt;span class="n"&gt;greeter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When you execute this script, you'll see the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;__name__ is: __main__
What&lt;span class="s1"&gt;'s your name?
&amp;gt; Adrian
Hi, Adrian!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The magic happens before the code runs. Python assigns the value &lt;code&gt;__main__&lt;/code&gt; to &lt;code&gt;__name__&lt;/code&gt; since the script is ran standalone.&lt;/p&gt;

&lt;h2&gt;
  
  
  Importing our greeter function
&lt;/h2&gt;

&lt;p&gt;What happens then if you import this script as a module from another script?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# client.py
&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;greeter&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;greet&lt;/span&gt;

&lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Jeff'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Annie'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Britta'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When you run this, you should see the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;__name__ is: greeter
Hi, Jeff!
Hi, Annie!
Hi, Britta!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since we imported &lt;code&gt;greeter&lt;/code&gt; as a module, Python assigned the name of the module, i.e. greeter, as the value of &lt;code&gt;__name__&lt;/code&gt;. Due to this, the code block inside &lt;code&gt;__main__&lt;/code&gt; never gets executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations! You made a Python script that you can execute standalone or as a module. All you had to do was to include the following &lt;code&gt;if&lt;/code&gt; statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
 &lt;span class="c1"&gt;# some code here
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can find the sample code used in this article right &lt;a href="https://github.com/adrianmarkperea/what-is-name-in-python"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As a recap, this works because when you run a Python script, the &lt;code&gt;__name__&lt;/code&gt; dunder variable is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;__main__&lt;/code&gt; if you run the script from the command line&lt;/li&gt;
&lt;li&gt;The name of the module if you import it from another script&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope this short tutorial was helpful!&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>7 Simple Ways to Increase Your Productivity</title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Sat, 27 Jun 2020 12:08:19 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/7-simple-ways-to-increase-your-productivity-2ne4</link>
      <guid>https://dev.to/adrianmarkperea/7-simple-ways-to-increase-your-productivity-2ne4</guid>
      <description>&lt;p&gt;&lt;em&gt;This post already appeared on &lt;a href="https://www.adrianperea.dev/seven-simple-ways-to-increase-your-productivity/"&gt;adrianperea.dev&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are only so many hours in the day, so how can you make sure you're making the most of your time?&lt;/p&gt;

&lt;p&gt;It's easy to get lost in a sea of code (even clean code!) and completely lose track of time. That's why before tackling any coding project, you need a plan of attack.&lt;/p&gt;

&lt;p&gt;This post will walk you through 9 simple tools and strategies to boost your productivity.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Avoid Mental Mapping when Trying to Understand Code
&lt;/h2&gt;

&lt;p&gt;You might think that you're good at juggling things in memory. But research has shown that we can only have up to &lt;a href="https://thebrain.mcgill.ca/flash/capsules/experience_jaune03.html#:~:text=Short%2DTerm%20Memory%3A%20Up%20to%207%20Items%2C%20But%20Highly%20Volatile&amp;amp;text=Many%20psychology%20experiments%20have%20shown,2%2C%20depending%20on%20the%20individual."&gt;7 items in our short-term memory&lt;/a&gt; at a time. So avoid mental mapping! Don't try to keep everything in your head when trying to understand code.&lt;/p&gt;

&lt;p&gt;Write it down. Draw diagrams. Draw flow charts. Pen and paper works best for me. Find out what works for you!&lt;/p&gt;

&lt;p&gt;Make sure your brain is doing what it's best at: processing. Leave everything else to auxiliary tools.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Practice Whiteboard Programming
&lt;/h2&gt;

&lt;p&gt;Whiteboard programming is the practice of solving a problem outside your text editor. You do this by writing down what you're trying to solve and breaking it down to individual steps. You can write it using pseudocode or natural speech. Whatever works for you!&lt;/p&gt;

&lt;p&gt;But why do this? Whiteboard programming offers the following benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It makes sure that you understand the problem completely&lt;/li&gt;
&lt;li&gt;It allows you to break the problem to individual steps&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you know the steps to solve your problem, everything else is a matter of syntax. Programming, then, will be a breeze.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Write Learning Tests for Third Party Libraries
&lt;/h2&gt;

&lt;p&gt;Learning tests are exactly what they sound like. They're tests that help you learn third party libraries.&lt;/p&gt;

&lt;p&gt;Writing tests for libraries may seem anti-productive (how can more work help productivity?) but it's not! Writing tests assures your understanding of the library.&lt;/p&gt;

&lt;p&gt;Once you start using it in your production code, you can make sure that everything works as you would expect. So, you will be able to write your code with confidence.&lt;/p&gt;

&lt;p&gt;As an added benefit, if a library gets updated, you can run the tests and immediately see if anything breaks.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Leverage Creative Momentum
&lt;/h2&gt;

&lt;p&gt;An object at motion stays in motion! Program a lot, and keep on building! The more you program, the more your mind gets attuned to programming, the better/faster you code!&lt;/p&gt;

&lt;h2&gt;
  
  
  5. But also take breaks. Use the Pomodoro Technique
&lt;/h2&gt;

&lt;p&gt;Not all productivity is active productivity. If you're tired, the most productive thing to do is to rest. If you're stuck at a problem, the best way to solve it is by taking a walk to clear your mind.&lt;/p&gt;

&lt;p&gt;The pomodoro technique allows you to systemize this. The technique uses a timer to break work into 25-minute intervals followed by a short rest. After four work intervals, you get to take a longer rest. A short rest usually lasts 5 minutes, and a long one lasts 25 minutes.&lt;/p&gt;

&lt;p&gt;If you followed tip #2 and broke down your problems into steps, it takes no effort at all to map them to work intervals. For longer tasks, it helps to assign more than one work interval.&lt;/p&gt;

&lt;p&gt;I hear from a lot of other programmers that 25 minutes is sometimes too short of a work interval. That's very true. Once we get into a state of momentum (tip #4), it can be counter-productive to force ourselves to stop.&lt;/p&gt;

&lt;p&gt;If 25 minutes is too short for you, the good news is you don't have to follow pomodoro to a fault. But you should learn from its principle: take breaks! Your mind will thank you for it.&lt;/p&gt;

&lt;p&gt;I use &lt;a href="https://www.focustodo.cn/#products"&gt;Focus To-do&lt;/a&gt; for my daily pomodoro needs. But there are already a lot of other apps out there for different platforms.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Manual Time Tracking
&lt;/h2&gt;

&lt;p&gt;Manual time tracking is no more than tracking how much time you take to perform different tasks. By doing manual time tracking, you get better at estimating how long tasks take.&lt;/p&gt;

&lt;p&gt;This is essential, because then you can get insights like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How much time you've actually spent on a project&lt;/li&gt;
&lt;li&gt;Which tasks take most of your time&lt;/li&gt;
&lt;li&gt;Why do some tasks take a lot of your time (are you getting distracted? Do you need more practice doing that particular task?)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you know patterns in your behavior, you can then make steps in knowing what problems to solve!&lt;/p&gt;

&lt;p&gt;You can use any application that you like that can keep track of time. I use Clockify. It can break down tasks into projects, and offers free analytics.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Automatic Time Tracking
&lt;/h2&gt;

&lt;p&gt;Manual time tracking is for general tasks. Automatic time tracking is for programming.&lt;/p&gt;

&lt;p&gt;Automatic time tracking tracks the following:&lt;br&gt;
How much time you spend programming&lt;br&gt;
Which languages do you spend most time on&lt;br&gt;
Which IDE do you use most of the time&lt;br&gt;&lt;br&gt;
I found out through this technique that I spend more time on CSS than in JavaScript. This made me realize that I needed to step up my CSS game!&lt;/p&gt;

&lt;p&gt;I use &lt;a href="https://wakatime.com"&gt;WakaTime&lt;/a&gt; to do automatic time tracking. It integrates with a ton of IDEs and text editors. For Visual Studio Code, all you need to do is sign up (for free) and download the extension. After that, you'll be able to see your stats in their web dashboard.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus!
&lt;/h2&gt;

&lt;p&gt;As a bonus tip, the best -- and possibly hardest -- way to keep yourself a productive developer is to be kind to yourself.&lt;/p&gt;

&lt;p&gt;Our jobs are difficult! There will be days that things will be overwhelming. There will be days you will feel imposter syndrome. During these days, the last thing you need is kicking yourself while you're down.&lt;/p&gt;

&lt;p&gt;Take a step back, take a deep breath, and relax. You're doing fine.&lt;/p&gt;

&lt;p&gt;Keep on coding. We’re all waiting for your next big thing!&lt;/p&gt;

</description>
      <category>productivity</category>
    </item>
    <item>
      <title>Quick! Share your awesome coding tunes!</title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Tue, 23 Jun 2020 17:11:10 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/quick-share-your-awesome-coding-tunes-16gf</link>
      <guid>https://dev.to/adrianmarkperea/quick-share-your-awesome-coding-tunes-16gf</guid>
      <description>&lt;p&gt;Mine may be kind of &lt;em&gt;basic&lt;/em&gt;, but I love &lt;a href="https://open.spotify.com/artist/38DX4hQVvPBs3PThDIAK11"&gt;Carbon Based Lifeforms&lt;/a&gt; and &lt;a href="https://open.spotify.com/artist/079svMEXkbT5nGU2kfoqO2"&gt;God is an Astronaut&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;I find myself too distracted if the music has lyrics or any flashy melodies, so I listen mostly to post-rock tunes and instrumentals. &lt;/p&gt;

&lt;p&gt;However, whenever I find myself strangely pumped, I put on something a little more upbeat like &lt;a href="https://open.spotify.com/artist/6Q192DXotxtaysaqNPy5yR"&gt;Amy Winehouse&lt;/a&gt; or &lt;a href="https://open.spotify.com/artist/0du5cEVh5yTK9QJze8zA0C"&gt;Bruno Mars&lt;/a&gt;. It steals my focus but I tend to enjoy more 😅&lt;/p&gt;

&lt;p&gt;What's &lt;em&gt;your&lt;/em&gt; coding 🎶🎶🎶?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Watch AI Evolve to Play Flappy Bird</title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Sun, 21 Jun 2020 10:01:46 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/watch-ai-evolve-to-play-flappy-bird-49o9</link>
      <guid>https://dev.to/adrianmarkperea/watch-ai-evolve-to-play-flappy-bird-49o9</guid>
      <description>&lt;p&gt;AI evolves to play flappy bird using genetic algorithms and neural networks 🎉&lt;/p&gt;

&lt;p&gt;Assets are still in beta, but happy that the data model and UI interaction are finally done!&lt;/p&gt;

&lt;p&gt;Here's a demo:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/SswVxUdVPWIsvpSyeg/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/SswVxUdVPWIsvpSyeg/giphy.gif" alt="Demo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see the site live right &lt;a href="https://vigilant-kepler-de0210.netlify.app/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For the curious, pre-compilation ones and zeros are &lt;a href="https://github.com/adrianmarkperea/flappy-bird-genetic-algo-js"&gt;here&lt;/a&gt;. 😎&lt;/p&gt;

&lt;p&gt;Let me know in the comments if you want a step-by-step on how I did it!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>showdev</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Of Classes and Constructor Functions: How JavaScript is Different from Other OOP Languages</title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Thu, 18 Jun 2020 16:07:35 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/of-classes-and-constructor-functions-how-javascript-is-different-from-other-oop-languages-4l35</link>
      <guid>https://dev.to/adrianmarkperea/of-classes-and-constructor-functions-how-javascript-is-different-from-other-oop-languages-4l35</guid>
      <description>&lt;p&gt;&lt;em&gt;This post originally appeared on &lt;a href="https://adrianperea.dev"&gt;adrianperea.dev&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/gnio/eli5-functions-vs-class-constructor-in-javascript-nki"&gt;A question was raised&lt;/a&gt; about the difference between functions and constructor functions in JavaScript. The question follows JavaScript's notorious reputation of &lt;em&gt;not&lt;/em&gt; being a real Object Oriented language. &lt;/p&gt;

&lt;p&gt;And while this is true (which we will get into later), popular literature mostly explains why in comparison with traditional OOP languages like C++, Java, or Python. Not only is this not helpful, it's also confusing for those who aren't familiar with those languages.&lt;/p&gt;

&lt;p&gt;So in this article, I will try to clear up how JavaScript classes are different from traditional OOP classes. I will be using Python as a representative of those languages because it's easy to understand and its relatively close to JavaScript. &lt;/p&gt;

&lt;h2&gt;
  
  
  Traditional OOP Languages
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;class&lt;/code&gt; is often defined as a blueprint for for objects. It serves two practical purposes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Abstraction&lt;/strong&gt;: which information is relevant? Which is irrelevant?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encapsulation&lt;/strong&gt;: how do I show or hide what is relevant or irrelevant?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At its very core, a &lt;code&gt;class&lt;/code&gt; has two types of properties: &lt;code&gt;members&lt;/code&gt; and &lt;code&gt;methods&lt;/code&gt;. These properties define the data stored in the &lt;code&gt;class&lt;/code&gt; and what operations the &lt;code&gt;class&lt;/code&gt; can do on that data.&lt;/p&gt;

&lt;p&gt;To make use of a &lt;code&gt;class&lt;/code&gt;, we create &lt;code&gt;instances&lt;/code&gt; of the class through a process called instantiation. Each &lt;code&gt;instance&lt;/code&gt; gets &lt;em&gt;isolated&lt;/em&gt; copies of the &lt;code&gt;members&lt;/code&gt; and &lt;code&gt;methods&lt;/code&gt; of the &lt;code&gt;class&lt;/code&gt;. Let's see how this works in Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_full_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;person_a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Adrian'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Perea'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;person_b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Ben'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'Halpern'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;person_a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;print_full_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# Adrian Perea
&lt;/span&gt;&lt;span class="n"&gt;person_b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;print_full_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# Ben Halpern
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;person_a&lt;/code&gt; and &lt;code&gt;person_b&lt;/code&gt; are &lt;code&gt;instances&lt;/code&gt; of &lt;code&gt;Person&lt;/code&gt;. Each of them gets their own &lt;code&gt;first_name&lt;/code&gt; and &lt;code&gt;last_name&lt;/code&gt; members, and their own &lt;code&gt;print_full_name&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Now in Python, you perform instantiation by just calling the &lt;code&gt;class&lt;/code&gt; directly (like how we created &lt;code&gt;person_a&lt;/code&gt; and &lt;code&gt;person_b&lt;/code&gt;). Traditionally however, this wasn't always the case. In C++ and Java, for example, you need to add the keyword &lt;code&gt;new&lt;/code&gt; in order to be able to instantiate the &lt;code&gt;class&lt;/code&gt;. I believe that this is where the confusion starts.&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript
&lt;/h2&gt;

&lt;p&gt;In JavaScript, we have something called &lt;strong&gt;constructor functions&lt;/strong&gt; that we called with the &lt;code&gt;new&lt;/code&gt; keyword. These constructor functions are the JavaScript analog of the class. Now while it seems that this is the same thing as the other languages we've mentioned, JavaScript behaves differently whenever we use these constructor functions. See, whenever we use the &lt;code&gt;new&lt;/code&gt; keyword to execute a constructor function, we're essentially telling JavaScript to run the function normally, but with two extra steps behind the scenes: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An implicit object is created at the start of the function that we can reference with &lt;code&gt;this&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The resulting instance has a copy of the constructor function's prototype property inside its own prototype.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Don't worry about the details for now as we'll get to those later. Let's see first how we can make a JavaScript object without any fancy constructor functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;personA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Adrian&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Perea&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;personB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ben&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Halpern&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;personA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Adrian Perea&lt;/span&gt;
&lt;span class="nx"&gt;personB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Ben Halpern&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This works fully well! Why not call it a day and be done with it?&lt;/p&gt;

&lt;p&gt;Well, the brutally honest truth is, we &lt;em&gt;can&lt;/em&gt;. There are a lot of things that we can accomplish by simply creating objects this way. But in so doing, we're missing the entire point of JavaScript being what we call a prototype-based language. This is what makes it unique (not necessarily better nor worse) from the traditional OOP languages.&lt;/p&gt;

&lt;p&gt;Now let's see how we can implement this another way. While you're reading the following snippet, remember the extra two steps that happen behind the scenes when constructor functions are called with &lt;code&gt;new&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// 1. An implicit object is created that we can reference with `this`&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// 2. The resulting instance has a copy of the &lt;/span&gt;
&lt;span class="c1"&gt;// constructor function's prototype property &lt;/span&gt;
&lt;span class="c1"&gt;// inside its own prototype. &lt;/span&gt;
&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;personA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Adrian&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Perea&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;personB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ben&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Halpern&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;personA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Adrian Perea&lt;/span&gt;
&lt;span class="nx"&gt;personB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Ben Halpern&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now this is where the magic happens. As you can see, when we created the &lt;code&gt;Person&lt;/code&gt; class, we separated where we defined the members (&lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt;) and where we defined the method (&lt;code&gt;fullName&lt;/code&gt;). &lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt; are right where you expect them: inside the constructor function definition. But the interesting part is where we define &lt;code&gt;fullName&lt;/code&gt; and that's in the &lt;code&gt;prototype&lt;/code&gt; of the constructor function.&lt;/p&gt;

&lt;p&gt;Why is this important? It's important because &lt;strong&gt;whenever we create a new &lt;code&gt;instance&lt;/code&gt; of the &lt;code&gt;Person&lt;/code&gt; constructor function through the &lt;code&gt;new&lt;/code&gt; keyword, a reference to the &lt;code&gt;prototype&lt;/code&gt; property of constructor function gets added to the &lt;code&gt;__proto__&lt;/code&gt; property of the object.&lt;/strong&gt; Read that again. After that, read it one more time. This part is important.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;personA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__proto__&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As opposed to traditional OOP languages, methods are not copied to each instance of the constructor function (or class). When we call &lt;code&gt;personA.fullName()&lt;/code&gt;, instead of finding the method in the instance itself, JavaScript looks at the &lt;code&gt;__proto__&lt;/code&gt; property of &lt;code&gt;personA&lt;/code&gt; and &lt;em&gt;climbs&lt;/em&gt; up until it finds &lt;code&gt;fullName&lt;/code&gt;. Since we defined &lt;code&gt;fullName&lt;/code&gt; in &lt;code&gt;Person.prototype&lt;/code&gt;, and since &lt;code&gt;Person.prototype&lt;/code&gt; is the same as &lt;code&gt;personA.__proto__&lt;/code&gt;, when we call &lt;code&gt;personA.fullName()&lt;/code&gt;, we're calling a method that exists not in the instance but in the constructor function itself! This provides performance benefits since the methods only have to be defined once (on the prototype of the constructor function). That's to say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;personA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;personB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This means that whatever we define on &lt;code&gt;Person.prototype&lt;/code&gt; will be available to all instances of &lt;code&gt;Person&lt;/code&gt;. In effect, we can do something weird (in traditional OOP sense) like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hi! I'm &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Note that we did not recreate the objects here&lt;/span&gt;
&lt;span class="nx"&gt;personA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hi! I'm Adrian&lt;/span&gt;
&lt;span class="nx"&gt;personB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hi! I'm Ben&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So there you have it. To sum things up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Constructor functions do two things in the background whenever they are called with &lt;code&gt;new&lt;/code&gt;: create an implicit object that can be referenced with &lt;code&gt;this&lt;/code&gt;, and assign the &lt;code&gt;__proto__&lt;/code&gt; property of each instance to refer to the &lt;code&gt;prototype&lt;/code&gt; property of the constructor function&lt;/li&gt;
&lt;li&gt;When a function is called on the instance, the &lt;code&gt;__proto__&lt;/code&gt; property is climbed until a reference to the called function is found. This means that each instance doesn't have a reference to the method, but all share the same method that's defined on the constructor function.&lt;/li&gt;
&lt;li&gt;In traditional OOP, all instances have a copy of each method. There is no concept of prototypes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What about ES6 "classes"
&lt;/h2&gt;

&lt;p&gt;ES6 "classes" don't really introduce the classes as we traditionally know them. It makes writing constructor functions easier since you wouldn't have to write &lt;code&gt;prototype&lt;/code&gt; for each method you want to share amongst instances. ES6 class syntax is simply an easier way to store all members and methods of a constructor function all in one place, while also abstracting &lt;code&gt;prototype&lt;/code&gt; and all the confusion it brings.&lt;/p&gt;

&lt;p&gt;As an example, we can write the &lt;code&gt;Person&lt;/code&gt; constructor function the following way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can see that it looks very similar to our python example (but you and I both know they're not the same!). Try creating instances of the &lt;code&gt;Person&lt;/code&gt; and look at the &lt;code&gt;prototype&lt;/code&gt; property yourself! 😉&lt;/p&gt;




&lt;p&gt;Hi! I'm Adrian, and I'm a software engineer. I work hard to provide helpful and highly intuitive content for free. If you like what you read, check out my &lt;a href="https://adrianperea.dev"&gt;blog&lt;/a&gt; or &lt;a href="https://twitter.com/adrianmarkperea"&gt;follow&lt;/a&gt; me on Twitter. Hope to see you again next time!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>oop</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What CSS tip do you want to share with others?</title>
      <dc:creator>Adrian Perea</dc:creator>
      <pubDate>Wed, 17 Jun 2020 09:55:06 +0000</pubDate>
      <link>https://dev.to/adrianmarkperea/what-css-tip-do-you-want-to-share-with-others-1a16</link>
      <guid>https://dev.to/adrianmarkperea/what-css-tip-do-you-want-to-share-with-others-1a16</guid>
      <description>&lt;p&gt;The world of CSS is very wild. Let's help each other out by sharing things that give it some order!&lt;/p&gt;

&lt;p&gt;I'll start!&lt;/p&gt;

&lt;p&gt;You can ditch media queries for dynamically changing font-sizes by use of fluid typography. By using &lt;code&gt;min&lt;/code&gt;, &lt;code&gt;max&lt;/code&gt;, and &lt;code&gt;viewport units&lt;/code&gt;, we can dynamically change the &lt;code&gt;font-size&lt;/code&gt; and constrain so that they don't explode (or shrink).&lt;/p&gt;

&lt;p&gt;Let's see an example. Let's say you want your header to be 2rem on mobile and 4rem on larger displays. Here's how you use fluid typography to accomplish that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;{&lt;/span&gt;&lt;span class="n"&gt;MIN_SIZE&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;4vw&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;MAX_SIZE&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="nt"&gt;font-size&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;max&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;2rem&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;4vw&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="nt"&gt;4rem&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;On most cases, 1rem = 16px, so our minimum font-size is 32px. This means that when the viewport width is less than 800px (0.04 * 800px = 32px), we will always have 32px as our font-size. This is perfect for mobile. When the viewport width is greater than 800px, our font-size will dynamically change along with the viewport, but never exceed 4rem = 64px.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;4vw&lt;/code&gt; was just used as an example. You can change it to any value that suits your needs. &lt;/p&gt;

&lt;p&gt;To see this in action, try changing the viewport width of the pen below. I changed &lt;code&gt;4vw&lt;/code&gt; to &lt;code&gt;8vw&lt;/code&gt; to make the font-size increase faster (font-size acceleration?!):&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/adrianmarkperea/embed/PoZbGdR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;And that's it! In just one line of code, you can make your font-size responsive!&lt;/p&gt;

&lt;p&gt;I hope this simple trick helps you guys out.&lt;br&gt;
Share other awesome tips down below! 🎉&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>css</category>
      <category>webdev</category>
      <category>codepen</category>
    </item>
  </channel>
</rss>
