<?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: Carmine Scarpitta</title>
    <description>The latest articles on DEV Community by Carmine Scarpitta (@cscarpitta).</description>
    <link>https://dev.to/cscarpitta</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%2F683530%2F49919d7b-c50e-4879-80fb-aaf3defe6b31.png</url>
      <title>DEV Community: Carmine Scarpitta</title>
      <link>https://dev.to/cscarpitta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cscarpitta"/>
    <language>en</language>
    <item>
      <title>'is' vs '==' to Check if Two Elements are Equal in Python</title>
      <dc:creator>Carmine Scarpitta</dc:creator>
      <pubDate>Tue, 12 Apr 2022 22:50:52 +0000</pubDate>
      <link>https://dev.to/cscarpitta/is-vs-to-check-if-two-elements-are-equal-in-python-5d08</link>
      <guid>https://dev.to/cscarpitta/is-vs-to-check-if-two-elements-are-equal-in-python-5d08</guid>
      <description>&lt;p&gt;Python objects can be compared using two operators: &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;is&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;

&lt;p&gt;Compare &lt;code&gt;x&lt;/code&gt; to &lt;code&gt;None&lt;/code&gt; using &lt;code&gt;is&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&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;'Object x is None'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare &lt;code&gt;x&lt;/code&gt; to the empty string &lt;code&gt;''&lt;/code&gt; using &lt;code&gt;==&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&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;'x is an empty string'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Apparently the two operators &lt;code&gt;is&lt;/code&gt; and &lt;code&gt;==&lt;/code&gt; can be used interchangeably, but this is not the case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Difference between &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;is&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;==&lt;/code&gt; is a &lt;strong&gt;equality&lt;/strong&gt; operator. It is used to check if two objects are equal or not.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;is&lt;/code&gt; is an identity operator. It is used to check if two objects are actually the same object or not. In other words, it checks if the two objects share the same memory location.&lt;/p&gt;

&lt;h3&gt;
  
  
  When should we use 'is' and when '=='?
&lt;/h3&gt;

&lt;p&gt;In general, if you are comparing an object to a sigleton like &lt;strong&gt;None&lt;/strong&gt;, &lt;strong&gt;True&lt;/strong&gt; or &lt;strong&gt;False&lt;/strong&gt; you should &lt;u&gt;always&lt;/u&gt; use &lt;code&gt;is&lt;/code&gt;. There are some exceptions, but most of the time this is the case.&lt;/p&gt;

&lt;p&gt;For all the other object types (e.g. strings and numbers), using &lt;code&gt;is&lt;/code&gt; can lead to unexpected behavior. To compare these object types, you must always use &lt;code&gt;==&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusions
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Data Type&lt;/th&gt;
&lt;th&gt;Compare using&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;&lt;code&gt;is&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bool&lt;/td&gt;
&lt;td&gt;&lt;code&gt;is&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;str&lt;/td&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;int&lt;/td&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;float&lt;/td&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;list&lt;/td&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;tuple&lt;/td&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;dict&lt;/td&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;bytes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;dict&lt;/td&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>2 reasons to use extend() instead of append() in Python</title>
      <dc:creator>Carmine Scarpitta</dc:creator>
      <pubDate>Sun, 10 Apr 2022 16:19:08 +0000</pubDate>
      <link>https://dev.to/cscarpitta/2-reasons-to-use-extend-instead-of-append-in-python-2kg2</link>
      <guid>https://dev.to/cscarpitta/2-reasons-to-use-extend-instead-of-append-in-python-2kg2</guid>
      <description>&lt;p&gt;Whether you are a Python noob or an expert, you have surely heard of &lt;strong&gt;list&lt;/strong&gt;. &lt;strong&gt;list&lt;/strong&gt; is one of Python's built-in data types.&lt;/p&gt;

&lt;p&gt;Creating an empty list in Python is very simple:&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;my_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have created an empty list, you can add items to it.&lt;/p&gt;

&lt;p&gt;Python offers two methods for adding items to a list: &lt;strong&gt;append()&lt;/strong&gt; and &lt;strong&gt;extend()&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Understanding how these methods work is important to choose which one to use.&lt;/p&gt;

&lt;h2&gt;
  
  
  append()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;append()&lt;/code&gt; adds an item to the end of the list. The item can be any Python object such as a &lt;em&gt;number&lt;/em&gt; or a &lt;em&gt;string&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The following example shows how to add the string &lt;code&gt;'hello'&lt;/code&gt; to the end of the &lt;code&gt;my_list&lt;/code&gt; list.&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;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'hello'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the &lt;code&gt;append()&lt;/code&gt; operation, &lt;code&gt;my_list&lt;/code&gt; will contain a single element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['hello']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  extend()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;extend()&lt;/code&gt; adds all the items of an iterable to the end of a list. An iterable is a Python object capable of returning its members one at a time. Examples of iterables are &lt;strong&gt;lists&lt;/strong&gt;, &lt;strong&gt;tuples&lt;/strong&gt; and &lt;strong&gt;strings&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The following example adds several items to the end of the &lt;code&gt;my_list&lt;/code&gt; list:&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;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'hello'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'world'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the &lt;code&gt;extend()&lt;/code&gt; operation &lt;code&gt;my_list&lt;/code&gt; will contain two elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;['hello', 'world']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Difference between &lt;code&gt;append()&lt;/code&gt; and &lt;code&gt;extend()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The main difference between &lt;code&gt;append()&lt;/code&gt; and &lt;code&gt;extend()&lt;/code&gt; is the number of items that can be added to the list. &lt;code&gt;append()&lt;/code&gt; adds only a single item to the list. Instead, &lt;code&gt;extend()&lt;/code&gt; can add all the items of an iterable to the list.&lt;/p&gt;

&lt;p&gt;To add many items to a list using &lt;code&gt;append()&lt;/code&gt;, we can define a loop, as shown in the following 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="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'hello'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'world'&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;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above &lt;code&gt;for&lt;/code&gt; loop iterates on the list of items and adds all the items to &lt;code&gt;my_list&lt;/code&gt; one by one. Basically for each iteration it adds an item of &lt;code&gt;items&lt;/code&gt; to &lt;code&gt;my_list&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Are &lt;code&gt;append()&lt;/code&gt; and &lt;code&gt;extend()&lt;/code&gt; equivalent?
&lt;/h2&gt;

&lt;p&gt;Not exactly. There are two differences:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;extend()&lt;/code&gt; makes the code more readable;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;extend()&lt;/code&gt; is more efficient than &lt;code&gt;append()&lt;/code&gt; (i.e. it requires less time to add the items to the list).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;extend()&lt;/code&gt; is more efficient?
&lt;/h2&gt;

&lt;p&gt;When we want to add items to the list, the Python interpreter needs to expand the list. In order to expand the list, it allocates new memory and copies the new items to the newly allocated memory. This is common to both &lt;code&gt;append()&lt;/code&gt; and &lt;code&gt;extend()&lt;/code&gt; operations.&lt;/p&gt;

&lt;p&gt;What happens when we add several items to a list using &lt;code&gt;append()&lt;/code&gt; in a loop?&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;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'hello'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'world'&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;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the Python interpreter iterates on each element of the items list. In each iteration, it allocates new memory in &lt;code&gt;my_list&lt;/code&gt;. The size of the allocated memory is such as to contain an element of &lt;code&gt;items&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First iteration: it allocates memory to store the &lt;code&gt;'hello'&lt;/code&gt; string and copies the &lt;code&gt;'hello'&lt;/code&gt; string to the newly allocated memory.&lt;/p&gt;

&lt;p&gt;Second iteration: it allocates memory to store the &lt;code&gt;'world'&lt;/code&gt; string and copies the &lt;code&gt;'world'&lt;/code&gt; string to the newly allocated memory.&lt;/p&gt;

&lt;p&gt;In conclusion, &lt;code&gt;append()&lt;/code&gt; performs two allocate operations and two copy operations.&lt;/p&gt;

&lt;p&gt;Now let's see what happens when we use &lt;code&gt;extend()&lt;/code&gt; to add all the items to &lt;code&gt;my_list&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="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'hello'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'world'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extend&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the Python interpreter allocates new memory to store both &lt;code&gt;'hello'&lt;/code&gt; and &lt;code&gt;'world'&lt;/code&gt; strings in &lt;code&gt;my_list&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, &lt;code&gt;extend()&lt;/code&gt; performs only one allocate operation.&lt;/p&gt;

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

&lt;p&gt;Adding many items to a list using &lt;code&gt;append()&lt;/code&gt; in a loop can require many allocate operations. In order to mitigate this inefficiency, Python developers implemented a over-allocation, which reduces the number of allocate operations required to append elements to a list. Over-allocation is out of scope for this post.&lt;/p&gt;

&lt;p&gt;Allocation operations can be very costly. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;extend()&lt;/code&gt; performs only one allocate operation, which results in an improved efficiency. Consequently, less time is required to add the items to the list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In conclusion, you should always use &lt;code&gt;extend()&lt;/code&gt; to add a set of items to a list.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's all for this post. If you liked this post, follow me on &lt;a href="https://dev.to/cscarpitta/"&gt;dev.to&lt;/a&gt; and Twitter (&lt;a href="https://twitter.com/cscarpitta94"&gt;@cscarpitta94&lt;/a&gt;) to get notified when I publish new posts. 😊&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>List vs Tuple - When To Use Each?</title>
      <dc:creator>Carmine Scarpitta</dc:creator>
      <pubDate>Thu, 07 Apr 2022 18:16:47 +0000</pubDate>
      <link>https://dev.to/cscarpitta/list-vs-tuple-when-to-use-each-4e99</link>
      <guid>https://dev.to/cscarpitta/list-vs-tuple-when-to-use-each-4e99</guid>
      <description>&lt;p&gt;In Python, &lt;strong&gt;lists&lt;/strong&gt; and &lt;strong&gt;tuples&lt;/strong&gt; are two built-in data types. Conceptually they are very similar, but there is some &lt;strong&gt;difference&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;First, the lists are &lt;strong&gt;mutable&lt;/strong&gt;. This means that once you have defined a list, you &lt;strong&gt;can&lt;/strong&gt; modify it.&lt;/p&gt;

&lt;p&gt;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;l&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;l&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="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'x'&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="n"&gt;l&lt;/span&gt;&lt;span class="p"&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;'x'&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;You can modify the list without any problems.&lt;/p&gt;

&lt;p&gt;On the contrary, tuples are &lt;strong&gt;immutable&lt;/strong&gt;. After creating a tuple, you &lt;strong&gt;cannot&lt;/strong&gt; modify it. If you try to modify a tuple you will get an error, as shown in the following 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="n"&gt;l&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="n"&gt;l&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="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'x'&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'tuple'&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;does&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;support&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="n"&gt;assignment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tuples have an advantage over lists. They are &lt;strong&gt;more memory efficient&lt;/strong&gt; and &lt;strong&gt;time efficient&lt;/strong&gt; than the lists. This means that using a tuple to store a set of items will require less memory than using a list to store the same set of items. Also, creating a tuple requires less time than creating a list.&lt;/p&gt;

&lt;p&gt;The following table summarizes the differences:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Lists&lt;/th&gt;
&lt;th&gt;Tuples&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Defined using square brackets&lt;/td&gt;
&lt;td&gt;Defined using rounded brackets&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;✔️ Lists are mutable&lt;/td&gt;
&lt;td&gt;❌ Tuples are immutable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;❌ Use more memory&lt;/td&gt;
&lt;td&gt;✔️ Use less memory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;❌ Slower&lt;/td&gt;
&lt;td&gt;✔️ Faster&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;Tuples are more &lt;strong&gt;memory&lt;/strong&gt; and &lt;strong&gt;time efficient&lt;/strong&gt; but cannot be modified. &lt;/p&gt;

&lt;p&gt;Therefore, if you have data that don't need to be changed, you should use tuples. Instead, if you need to change data you are forced to use lists.&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>6 Ways To Create An Object In JavaScript</title>
      <dc:creator>Carmine Scarpitta</dc:creator>
      <pubDate>Tue, 05 Apr 2022 21:36:24 +0000</pubDate>
      <link>https://dev.to/cscarpitta/8-ways-to-create-an-object-in-javascript-2h38</link>
      <guid>https://dev.to/cscarpitta/8-ways-to-create-an-object-in-javascript-2h38</guid>
      <description>&lt;h2&gt;
  
  
  What is an Object?
&lt;/h2&gt;

&lt;p&gt;Objects are one of JavaScript's data types. An object can be seen as a collection of properties. A property is a name-value pair.&lt;/p&gt;

&lt;p&gt;In other words, a JavaScript object is something very similar to a real-life object or entity.&lt;/p&gt;

&lt;p&gt;Think a person. A person has different properties like the name or the age. Well... a person can be modeled as a JavaScript object.&lt;/p&gt;

&lt;p&gt;There are many ways to create an object in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Object() Constructor
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Use the &lt;code&gt;Object()&lt;/code&gt; constructor to create an empty object.&lt;/li&gt;
&lt;li&gt;Set the object's properties.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&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="c1"&gt;// Use the `Object()` constructor to create an empty object&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Set the object's properties&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Create Method
&lt;/h2&gt;

&lt;p&gt;Object.create() can be used to create an object from the prototype of another object.&lt;/p&gt;

&lt;p&gt;Example:&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Object Initializer
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Write the curly brackets {}.&lt;/li&gt;
&lt;li&gt;Put all properties (&lt;code&gt;name: value&lt;/code&gt; pairs) inside the curly brackets.&lt;/li&gt;
&lt;li&gt;Add commas to separate &lt;code&gt;name: value&lt;/code&gt; pairs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is called Object literal (or Object initializer) and can be used to create an object.&lt;/p&gt;

&lt;p&gt;Example:&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Constructor Function
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a Constructor Function.&lt;/li&gt;
&lt;li&gt;Use the 'new' keyword to create an object from the constructor function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&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="c1"&gt;// Create a Constructor Function&lt;/span&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="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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Use the 'new' keyword to create an object from the constructor function&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. ES6 Class (only for JavaScript ES6)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a ES6 class containing a constructor method.&lt;/li&gt;
&lt;li&gt;The constructor takes the property values of the object as arguments.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&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="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;name&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="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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&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;var&lt;/span&gt; &lt;span class="nx"&gt;person&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="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. Singleton Object
&lt;/h2&gt;

&lt;p&gt;A Singleton is an object that can be created only one time. An attempt to create a second object will return a reference to the first object.&lt;/p&gt;

&lt;p&gt;Example:&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kd"&gt;function&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&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;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this blog post we have shown 6 different ways to create an object in JavaScript. Every developer should be aware of these ways and choose the most appropriate way from time to time.&lt;/p&gt;

&lt;p&gt;That's all for this post. If you liked this post, follow me on &lt;a href="https://dev.to/cscarpitta/"&gt;dev.to&lt;/a&gt; and Twitter (&lt;a href="https://twitter.com/cscarpitta94"&gt;@cscarpitta94&lt;/a&gt;) to get notified when I publish new posts. 😊&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Creating a Simple Weather App with Python and Flask</title>
      <dc:creator>Carmine Scarpitta</dc:creator>
      <pubDate>Mon, 04 Apr 2022 00:21:15 +0000</pubDate>
      <link>https://dev.to/cscarpitta/creating-a-simple-weather-app-with-python-and-flask-5bpd</link>
      <guid>https://dev.to/cscarpitta/creating-a-simple-weather-app-with-python-and-flask-5bpd</guid>
      <description>&lt;p&gt;Are you learning Python? Practicing Flask? And tired of the usual Hello World?&lt;/p&gt;

&lt;p&gt;Well! Then this post is for you.&lt;/p&gt;

&lt;p&gt;In this post we will build a weather app in Python using Flask.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Overview ☁️
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Flask&lt;/strong&gt; is a framework for building &lt;strong&gt;web applications&lt;/strong&gt; with Python. We will develop a web application that provides the weather forecast for 5 days.&lt;/p&gt;

&lt;p&gt;Flask makes the task of creating a web application relatively simple. Our weather app will require no more than &lt;strong&gt;22 lines&lt;/strong&gt; of Python code and &lt;strong&gt;48 lines&lt;/strong&gt; of HTML.&lt;/p&gt;

&lt;h2&gt;
  
  
  An example ☀️
&lt;/h2&gt;

&lt;p&gt;We need a web server to host our application. &lt;/p&gt;

&lt;p&gt;An example of how our app will work:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The user will open a browser and navigate to &lt;a href="http://localhost:5000/forecast?city=London" rel="noopener noreferrer"&gt;http://localhost:5000/forecast?city=London&lt;/a&gt; (like "Tell me the weather forecast for London").&lt;/li&gt;
&lt;li&gt;The web server will receive the request and call our web app to retrieve the weather forecast. &lt;/li&gt;
&lt;li&gt;Finally, the web server will send an HTML page containing the weather forecast back to the user.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The user will receive the HTML page and display the forecast for London in the browser.&lt;/p&gt;

&lt;p&gt;There are many different web servers like NGINX or Apache HTTP Server. We will use the Flask's built-in server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing weather forecast ❄️
&lt;/h2&gt;

&lt;p&gt;You may be wondering: where will our weather app get the forecast data from?&lt;/p&gt;

&lt;p&gt;Well, there are many different weather services available. We will use &lt;strong&gt;OpenWeatherMap&lt;/strong&gt; service. It supports different subscription plans. We will use the free plan that supports up to 60 calls/minute and 1,000,000 calls/month which are sufficient for the purpose of this tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the weather app ⚡
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Before starting to code, we need to install &lt;strong&gt;Flask&lt;/strong&gt;. To install Flask, we open a terminal and execute 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;pip &lt;span class="nb"&gt;install &lt;/span&gt;flask
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The installation requires few seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Project Structure
&lt;/h3&gt;

&lt;p&gt;We structure the project as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── static
│   └── icons
|       ├── 01d.png
|       ├── 01n.png
|       ├── 02d.png
|       └── ...
├── templates
│   └── index.html
├── weather.py
└── README.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;weather.py&lt;/code&gt; we will put the Flask application.&lt;/p&gt;

&lt;p&gt;The folder &lt;code&gt;static/icons&lt;/code&gt; will contain all the icons used to represent the weather conditions. For example, we will have an icon for Clouds, another icon for Sunny, and so on.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;templates&lt;/code&gt; is a folder containing a single file &lt;code&gt;index.html&lt;/code&gt;. This HTML file only contains an empty table where we will put the weather data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sign up to OpenWeatherMap
&lt;/h3&gt;

&lt;p&gt;Before moving on, we need to sign up to OpenWeatherMap.&lt;/p&gt;

&lt;p&gt;Visit &lt;a href="https://openweathermap.org/api" rel="noopener noreferrer"&gt;https://openweathermap.org/api&lt;/a&gt; and click on the &lt;strong&gt;Subscribe&lt;/strong&gt; button under the &lt;strong&gt;5 Day / 3 Hour Forecast&lt;/strong&gt; section:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexnk1abm04gqn5c03vxp.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexnk1abm04gqn5c03vxp.png" alt="5 Day / 3 Hour Forecast"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on &lt;strong&gt;Get API key&lt;/strong&gt; and complete the registration:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qgu4egrqsle8rwcn9df.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qgu4egrqsle8rwcn9df.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, log into OpenWeatherMap website with your credentials and click on &lt;strong&gt;My API keys&lt;/strong&gt;. Follow the instrutions to generate a new API key.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing the Weather App
&lt;/h3&gt;

&lt;p&gt;In &lt;code&gt;weather.py&lt;/code&gt; we put the logic that receives user requests and provides weather forecast. &lt;/p&gt;

&lt;p&gt;We need to create a function that retrieves the weather data from OpenWeatherMap API. This function is invoked when the user sends an HTTP GET request to our web app (for example &lt;a href="http://localhost:5000/forecast?city=London" rel="noopener noreferrer"&gt;http://localhost:5000/forecast?city=London&lt;/a&gt;) and returns an HTML page containing the weather forecast.&lt;/p&gt;

&lt;p&gt;From &lt;code&gt;flask&lt;/code&gt; package, we need to import the Flask class and other functions that we will use later:&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="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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;render_template&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we initialize the Flask application:&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;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create a function called &lt;code&gt;get_weather()&lt;/code&gt; that will be executed when the user sends an HTTP GET request to the weather app. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This function will extract the city name from the request of the user.&lt;/li&gt;
&lt;li&gt;It will retrieve the weather for the requested city from OpenWeatherMap.&lt;/li&gt;
&lt;li&gt;Finally, an HTML page containing the weather is returned to the user.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The browser of the user will show the HTML page received from the web server. This HTML page contains a table showing the weather conditions for the requested city.&lt;/p&gt;

&lt;p&gt;Let's create the &lt;code&gt;get_weather()&lt;/code&gt; function step by step.&lt;/p&gt;

&lt;p&gt;We define a function &lt;code&gt;get_weather()&lt;/code&gt; that takes no arguments:&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="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;/forecast&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;GET&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;get_weather&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;@app.route('/forecast', methods=['GET'])&lt;/code&gt; is a decorator. It is a directive that routes the HTTP GET requests made to the &lt;a href="http://localhost/forecast" rel="noopener noreferrer"&gt;http://localhost/forecast&lt;/a&gt; URL to the &lt;code&gt;get_weather()&lt;/code&gt; function. In this way, when an user navigates to &lt;a href="http://localhost/forecast" rel="noopener noreferrer"&gt;http://localhost/forecast&lt;/a&gt; our Flask web server will execute the &lt;code&gt;get_weather()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;We want that the user specifies the city as an argument of the HTTP GET request. Therefore, we expect that the HTTP request contains an argument named &lt;em&gt;city&lt;/em&gt;. We can extract the city from the HTTP request 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;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;city&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;It is important to validate the parameters extracted from the HTTP request. In our case, city is a mandatory argument. We need to ensure that the user provided a city. If the request does not contain a city (i.e. &lt;code&gt;city is None&lt;/code&gt;), we return a status code "400" (BAD REQUEST) and we provide a description that tells the user that the city argument is missing:&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Missing argument city&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;If a city has been provided in the HTTP request, we prepare a request for OpenWeatherMap to get the weather forecast. The request will contain three parameters: &lt;code&gt;q&lt;/code&gt;, &lt;code&gt;appid&lt;/code&gt; and &lt;code&gt;units&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;q&lt;/code&gt; is the name of the city for which we want the forecasts. &lt;code&gt;appid&lt;/code&gt; is your OpenWeatherMap API key.&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;q&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;city&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;appid&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;XXXXXXXXX&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;units&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;metric&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use urllib python library to perform the retrieve the forecast from OpenWeatherMap:&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;url_values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlencode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;http://api.openweathermap.org/data/2.5/forecast&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="n"&gt;full_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&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="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;url_values&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;urllib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we use &lt;code&gt;render_template&lt;/code&gt; to generate the HTML page containing the results. Basically &lt;code&gt;render_template&lt;/code&gt; takes the template HTML file, fills it with the weather data received from OpenWeatherMap and return the generated HTML page to the user. Finally, the user will display the weather results in the browser:&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;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;render_template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;index.html&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Weather App&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;utf8&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;h3&gt;
  
  
  HTML Template file
&lt;/h3&gt;

&lt;p&gt;The HTML template file is reported below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;{{ title }}&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;table&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;arial&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;border-collapse&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;collapse&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;th&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#dddddd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="nt"&gt;tr&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;even&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#dddddd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="nf"&gt;#weather&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;table&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Time&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Temperature&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Humidity&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Weather&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Wind&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
            {% for item in data["list"] %}
                &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;{{ item["dt_txt"] }}&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;{{ item["main"]["temp"] }}°C&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;{{ item["main"]["humidity"] }}%&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"weather"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/static/icons/{{ item["&lt;/span&gt;&lt;span class="na"&gt;weather&lt;/span&gt;&lt;span class="err"&gt;"][0]["&lt;/span&gt;&lt;span class="na"&gt;icon&lt;/span&gt;&lt;span class="err"&gt;"]&lt;/span&gt; &lt;span class="err"&gt;}}.&lt;/span&gt;&lt;span class="na"&gt;png&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/img&amp;gt;&lt;/span&gt; {{ item["weather"][0]["main"] }}&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;{{ '%.2f' % (item["wind"]["speed"]*3.6) }} km/h ({{ item["wind"]["deg"] }} deg)&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
            {% endfor %}
        &lt;span class="nt"&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the final result:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frdrx3t3dgtc5znwcelvi.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frdrx3t3dgtc5znwcelvi.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;In this blog post we developed a simple weather app using Python and Flask. This is a great exercise for Python and Flask beginners.&lt;/p&gt;

&lt;p&gt;If you like this post, consider following me on Twitter (&lt;a href="https://twitter.com/cscarpitta94" rel="noopener noreferrer"&gt;https://twitter.com/cscarpitta94&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Credits for the images:&lt;br&gt;
&lt;a href="https://www.flaticon.com/free-icons/weather" title="weather icons" rel="noopener noreferrer"&gt;Weather icons created by iconixar - Flaticon&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>flask</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>6 CSS Shorthand properties to improve your web application</title>
      <dc:creator>Carmine Scarpitta</dc:creator>
      <pubDate>Sun, 22 Aug 2021 10:55:25 +0000</pubDate>
      <link>https://dev.to/cscarpitta/6-css-shorthand-properties-to-improve-your-web-application-2dbj</link>
      <guid>https://dev.to/cscarpitta/6-css-shorthand-properties-to-improve-your-web-application-2dbj</guid>
      <description>&lt;h1&gt;
  
  
  Why should I care shorthand properties? 🎈
&lt;/h1&gt;

&lt;p&gt;CSS is a language used to describe how a web page should look like. With CSS we can set position, colors, fonts, layout of each element in an HTML 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsuxzb0mwwowa8nbvmy7n.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsuxzb0mwwowa8nbvmy7n.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this blog post, I will share some tips to improve your CSS code and the performance of your web app. ⚠️&lt;/p&gt;

&lt;center&gt; 🔥 🌞 🌴 🍄 🔥 &lt;/center&gt;

&lt;p&gt;There are several ways in which a CSS file can be optimized. One of the things you should care when writing your CSS code is to minimize the number of lines. 💥&lt;/p&gt;

&lt;p&gt;There are several reasons to care about the number of code lines 😲:&lt;br&gt;
👉 improve the code readability&lt;br&gt;
👉 improve the loading speed of your web page&lt;br&gt;
👉 improve the ranking in search engines (e.g. Google Search, Bing, ...), because ranking depends on the loading speed and optimization level of your application&lt;/p&gt;

&lt;center&gt;🔥 🔥 🔥 🔥 🔥&lt;/center&gt;

&lt;p&gt;An interesting tip to reduce the number of lines of code is the following:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Use CSS shorthand properties whenever possible&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this blog post I'll show you what shorthand properties are and how they can be used to optimize your CSS code.&lt;/p&gt;

&lt;p&gt;Interested? Read on!!! 😊💻&lt;/p&gt;
&lt;h1&gt;
  
  
  What are shorthand properties?
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Shorthand properties&lt;/strong&gt; let you set the values of multiple other CSS properties simultaneously.&lt;/p&gt;

&lt;p&gt;CSS supports a number of shorthand properties. In this blog post we will see the most used ones. ✈️&lt;/p&gt;
&lt;h1&gt;
  
  
  CSS Background Shorthand
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Background property&lt;/strong&gt; lets you set different background properties of an HTML element (e.g. a &lt;code&gt;div&lt;/code&gt;) in a single line of CSS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Background&lt;/strong&gt; is a shorthand for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;background-color&lt;/li&gt;
&lt;li&gt;background-image&lt;/li&gt;
&lt;li&gt;background-position&lt;/li&gt;
&lt;li&gt;background-size&lt;/li&gt;
&lt;li&gt;background-repeat&lt;/li&gt;
&lt;li&gt;background-origin&lt;/li&gt;
&lt;li&gt;background-clip&lt;/li&gt;
&lt;li&gt;background-attachment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So instead of having background-color, background-image,  background-position, background-size, background-repeat, background-origin, background-clip, background-attachment defined for an HTML element, we can use a single property. 😍&lt;/p&gt;

&lt;p&gt;Hmmm confused??? 😱 😕 &lt;/p&gt;

&lt;p&gt;A picture is worth a thousand words:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzukoe629szz9kl9kng5a.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzukoe629szz9kl9kng5a.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's the trick. Thanks to &lt;code&gt;background&lt;/code&gt; shorthand property, we have compressed 8 CSS lines into one line.&lt;/p&gt;

&lt;p&gt;Now let's image a complex web application, with dozens of CSS files and thousands of lines per file. 😱&lt;/p&gt;

&lt;p&gt;Definitely, this results in much smaller files, cleaner code and faster loading times for your web application. 😄&lt;/p&gt;
&lt;h1&gt;
  
  
  CSS Border Shorthand
&lt;/h1&gt;

&lt;p&gt;The second shorthand property that I want to show you is called &lt;strong&gt;Border&lt;/strong&gt;. Border shorthand is used to set the border of an HTML element.&lt;/p&gt;

&lt;p&gt;It is a shorthand for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;border-width&lt;/li&gt;
&lt;li&gt;border-style&lt;/li&gt;
&lt;li&gt;border-color&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fua2jaajgkkzyzkujcekc.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fua2jaajgkkzyzkujcekc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Three properties at one go. Not so bad!!! 😏 🔥&lt;/p&gt;
&lt;h1&gt;
  
  
  CSS Font Shorthand
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Font&lt;/strong&gt; shorthand is used to set the following font properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;font-style&lt;/li&gt;
&lt;li&gt;font-variant&lt;/li&gt;
&lt;li&gt;font-weight&lt;/li&gt;
&lt;li&gt;font-size/line-height&lt;/li&gt;
&lt;li&gt;font-family&lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepbng7ypqeahp7wsnvbf.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fepbng7ypqeahp7wsnvbf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  CSS Inset Shorthand
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;Inset&lt;/strong&gt; property has to do with the positioning of an HTML element. It is a shorthand for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;top&lt;/li&gt;
&lt;li&gt;right&lt;/li&gt;
&lt;li&gt;bottom&lt;/li&gt;
&lt;li&gt;left&lt;/li&gt;
&lt;/ul&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ppsfx8coso1j5zsmyg9.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ppsfx8coso1j5zsmyg9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Simple and efficient! ☺️ 🌺&lt;/p&gt;
&lt;h1&gt;
  
  
  CSS Padding Shorthand
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Padding&lt;/strong&gt; is a way to add space around an element. More precisely it allows you to add space between the element and its border.&lt;/p&gt;

&lt;p&gt;To completely set the padding of an HTML element, we need to set four values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;padding-top&lt;/li&gt;
&lt;li&gt;padding-right&lt;/li&gt;
&lt;li&gt;padding-bottom&lt;/li&gt;
&lt;li&gt;padding-left&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The meaning of these values is quite intuitive:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;padding-top&lt;/strong&gt; is the space between the element and the its border&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;padding-right&lt;/strong&gt; is the space between the element and its right border&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;padding-bottom&lt;/strong&gt; is the space between the element and its bottom border&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;padding-left&lt;/strong&gt; is the space between the element and its left border&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All these properties can be specified in a single declaration, using the &lt;strong&gt;padding&lt;/strong&gt; shorthand. 🌻&lt;/p&gt;

&lt;p&gt;The syntax is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;padding&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;padding-top&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;padding-right&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;padding-bottom&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;padding-left&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;Here is an example:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpi10x5153xgp7lyg8kct.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpi10x5153xgp7lyg8kct.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  CSS Margin Shorthand
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Margin&lt;/strong&gt; property is similar to padding. Margin is the space around the element, outside its borders.&lt;/p&gt;

&lt;p&gt;To specify a margin you need to provide four different values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;margin-top&lt;/strong&gt; is the space between the top border of the element and the other elements&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;margin-right&lt;/strong&gt; is the space between the right border of the element and the other elements&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;margin-bottom&lt;/strong&gt; is the space between the bottom border of the element and the other elements&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;margin-left&lt;/strong&gt; is the space between the left border of the element and the other elements&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The syntax is straightforward 🌹:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;margin&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;margin-top&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;margin-right&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;margin-bottom&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;margin-left&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;Here is an example:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fay5vhdzzts34yyv060yh.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fay5vhdzzts34yyv060yh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;We have come to an end. 🌼 I want to remark the following tip:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Use CSS shorthand properties whenever possible&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;because they help to reduce the lines of CSS code and improve readability. &lt;/p&gt;

&lt;p&gt;Reducing the CSS file size will also &lt;strong&gt;improve the loading speed of our web pages&lt;/strong&gt;, because the CSS file is &lt;strong&gt;smaller&lt;/strong&gt;. This will also &lt;strong&gt;improve our ranking in search engine&lt;/strong&gt;, because the search engine algorithms tend to reward the optimized web pages. 🌈 🚀&lt;/p&gt;

&lt;center&gt;🔥 🔥 🔥 🔥 🔥&lt;/center&gt;

&lt;p&gt;I hope I convinced you to care about lines of CSS code. 😜&lt;/p&gt;

&lt;p&gt;If you liked this post, consider following me on Twitter &lt;a href="https://twitter.com/cscarpitta94" rel="noopener noreferrer"&gt;@cscarpitta94&lt;/a&gt; and on dev &lt;a href="https://dev.to/cscarpitta"&gt;cscarpitta&lt;/a&gt; 😍&lt;/p&gt;

&lt;center&gt;👋 🔥&lt;/center&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Build a simple Pie Chart with HTML and CSS</title>
      <dc:creator>Carmine Scarpitta</dc:creator>
      <pubDate>Sat, 14 Aug 2021 18:32:45 +0000</pubDate>
      <link>https://dev.to/cscarpitta/build-a-simple-pie-chart-with-html-and-css-32dn</link>
      <guid>https://dev.to/cscarpitta/build-a-simple-pie-chart-with-html-and-css-32dn</guid>
      <description>&lt;p&gt;You can create a &lt;strong&gt;Pie Chart&lt;/strong&gt; in HTML using a simple CSS function called &lt;code&gt;conic-gradient&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First, we add a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; element to our HTML page, which acts as a placeholder for our pie chart.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"my-pie-chart"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to provide a &lt;code&gt;width&lt;/code&gt; and a &lt;code&gt;height&lt;/code&gt; to the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; element, which determine the size of our pie chart:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nf"&gt;#my-pie-chart&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&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;Then, we need to make our pie chart circle-shaped by setting the &lt;code&gt;border-radius&lt;/code&gt; value to &lt;code&gt;50%&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nf"&gt;#my-pie-chart&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&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;And finally we are ready to populate the pie chart with our data. &lt;/p&gt;

&lt;p&gt;As an example, let's consider the world population data reported at the following link:&lt;br&gt;
&lt;a href="https://www.worldometers.info/geography/7-continents/"&gt;https://www.worldometers.info/geography/7-continents/&lt;/a&gt;&lt;br&gt;
We want to show the population distribution per continent using our pie chart.&lt;/p&gt;

&lt;p&gt;For each continent, we associate an arbitrary color and the population percentage taken from the above link. The data is summarized in the following table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Continent&lt;/th&gt;
&lt;th&gt;Color&lt;/th&gt;
&lt;th&gt;Population&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Asia&lt;/td&gt;
&lt;td&gt;red&lt;/td&gt;
&lt;td&gt;59.54%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Africa&lt;/td&gt;
&lt;td&gt;orange&lt;/td&gt;
&lt;td&gt;17.20%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Europe&lt;/td&gt;
&lt;td&gt;yellow&lt;/td&gt;
&lt;td&gt;9.59%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;North America&lt;/td&gt;
&lt;td&gt;green&lt;/td&gt;
&lt;td&gt;7.60%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;South America&lt;/td&gt;
&lt;td&gt;blue&lt;/td&gt;
&lt;td&gt;5.53%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Australia&lt;/td&gt;
&lt;td&gt;black&lt;/td&gt;
&lt;td&gt;0.55%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Antarctica&lt;/td&gt;
&lt;td&gt;brown&lt;/td&gt;
&lt;td&gt;0.00%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To apply these values to our pie chart, we need to partition it into 7 sectors, a sector for each continent. To create the sectors, we can use the &lt;code&gt;conic-gradient&lt;/code&gt; CSS function. Each sector has a color, a start position and a stop position.&lt;/p&gt;

&lt;p&gt;For example, Antarctica is represented by the brown color and has 0.00% of the world population. Therefore, we want a brown sector from 0.00% to 0.00%. &lt;br&gt;
Then, we want to plot a black sector representing the Australia, which has 0.55% of the world population. This results in a black sector going from 0.00% to 0.55%. &lt;br&gt;
Similarly, to represent the South America we want a blue sector going from 0.55% to 6.08% (= 0.55% + 5.53%). &lt;br&gt;
And so on.&lt;/p&gt;

&lt;p&gt;At the end we will have the following CSS background property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nf"&gt;#my-pie-chart&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;conic-gradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;brown&lt;/span&gt; &lt;span class="m"&gt;0.00%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;black&lt;/span&gt; &lt;span class="m"&gt;0.00%&lt;/span&gt; &lt;span class="m"&gt;0.55%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt; &lt;span class="m"&gt;0.55%&lt;/span&gt; &lt;span class="m"&gt;6.08%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;green&lt;/span&gt; &lt;span class="m"&gt;6.08%&lt;/span&gt; &lt;span class="m"&gt;13.68%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;yellow&lt;/span&gt; &lt;span class="m"&gt;13.68%&lt;/span&gt; &lt;span class="m"&gt;23.27%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;orange&lt;/span&gt; &lt;span class="m"&gt;23.27%&lt;/span&gt; &lt;span class="m"&gt;40.47%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt; &lt;span class="m"&gt;40.47%&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;🐛 That's all. Now we are able to create a pie chart in CSS.&lt;/p&gt;

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

</description>
      <category>css</category>
      <category>webdev</category>
      <category>html</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
