<?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: Paul Nasdaq</title>
    <description>The latest articles on DEV Community by Paul Nasdaq (@nasdaqpaul).</description>
    <link>https://dev.to/nasdaqpaul</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%2F814046%2F54d09834-e148-4c77-91aa-0efaf4d37502.png</url>
      <title>DEV Community: Paul Nasdaq</title>
      <link>https://dev.to/nasdaqpaul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nasdaqpaul"/>
    <language>en</language>
    <item>
      <title>Data Structures with Python</title>
      <dc:creator>Paul Nasdaq</dc:creator>
      <pubDate>Mon, 21 Feb 2022 18:58:48 +0000</pubDate>
      <link>https://dev.to/nasdaqpaul/data-structures-with-python-4m7e</link>
      <guid>https://dev.to/nasdaqpaul/data-structures-with-python-4m7e</guid>
      <description>&lt;p&gt;Python is a high level programming language. This is a good thing if you are new to data structures, as some of the most common data structures in computer science are in built, or can be imported from a module from the Python standard library. You won't have to implement them on your own, unless you are looking for special functionality&lt;br&gt;
Let us first describe what a data structure is. In layman terms, all a data structure is a collection of values. Usually these values have a relations, and you can access, use and manipulate these values from a single location, that is, a data structure.&lt;br&gt;
Data structures in come in different forms, with different rules as to how you can add, remove or change the values in the data structure.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Lists
&lt;/h2&gt;

&lt;p&gt;Lists probably the most popular and the simplest to understand data structure in Python. Values stored in a list are called the elements of a list. A list can be visualized as a sequence of elements. Elements in a list have a position, called an &lt;strong&gt;index&lt;/strong&gt;. Elements can only be accessed by their position in the list i.e their index&lt;br&gt;
Indexes are numerical, and start from 0 to n-1, where n is the number of elements in the list. So, for example, if we have a list that has 10 elements, the elements will have an index of 0 to 9, (not 1 to 10).&lt;br&gt;
In Python, there is no restriction as to what type of elements can be stored in a particular list. There is also no restriction as to how many elements a list can contain. They can be as small or as large as you may wish.&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;# Creating an empty list
&lt;/span&gt;&lt;span class="n"&gt;myList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="c1"&gt;# Creating a list with items
&lt;/span&gt;&lt;span class="n"&gt;myOtherList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'one'&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="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'three'&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;

&lt;span class="c1"&gt;# Accessing items in a list
&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;myOtherList&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;         &lt;span class="c1"&gt;#'one'
&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;myOtherList&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="c1"&gt;#2
&lt;/span&gt;
&lt;span class="c1"&gt;# Modifying a list item
&lt;/span&gt;&lt;span class="n"&gt;myOtherList&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="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;myOtherList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;#['one', 2, 3]
&lt;/span&gt;
&lt;span class="c1"&gt;# Adding items to a list
&lt;/span&gt;&lt;span class="n"&gt;myList&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;'aValue'&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;myList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                 &lt;span class="c1"&gt;#['aValue']
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Dictionaries
&lt;/h2&gt;

&lt;p&gt;Ptyhon dictionaries are quite a neat data structure. Items in a dictionary are accessed by a &lt;em&gt;name&lt;/em&gt; (more specifically called a &lt;strong&gt;key&lt;/strong&gt;). The key is set when you insert the item into the dictionary. Just like lists, there is no restriction as to the type or number of items a dictionary can store. As for the keys, any 'immutable type' can be used. These include strings, numbers and tuples.&lt;br&gt;
Python dictionaries are synonymous to Maps or Associative Arrays from other programming languages. They are implemented using &lt;strong&gt;hash tables&lt;/strong&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;# Creating an empty dictionary
&lt;/span&gt;&lt;span class="n"&gt;myDict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="c1"&gt;# Creating a dictionary with initial values
&lt;/span&gt;&lt;span class="n"&gt;personDict&lt;/span&gt; &lt;span class="o"&gt;=&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="s"&gt;'Paul Nasdaq'&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="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Accessing items in a dictionary
&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;personDict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;# 'Paul Nasdaq'
&lt;/span&gt;&lt;span class="n"&gt;personDict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'age'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;24&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;personDict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# {name: 'Paul Nasdaq', age: 24}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Tuples
&lt;/h2&gt;

&lt;p&gt;A tuple is a data structure that once created, cannot be changed (modified). That is, you cannot add, remove or change items in a tuple once it has been created. Tuples are useful for defining &lt;em&gt;compound values&lt;/em&gt;. A compound values is a value that is made up of two or more other values. For example, a geographical location is usually made up of a longitude and a latitude. A tuple would be an excellent choice to store geographical locations in your program. It wouldn't make sense to be able to modify the longitude or latitude of a location once created, because a different value is a totally different location. Just like lists, items in a tuple can be accessed by index.&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;# Creating tuples
&lt;/span&gt;&lt;span class="n"&gt;myTuple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'one'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;two&lt;/span&gt;&lt;span class="s"&gt;')
myOtherTuple = '&lt;/span&gt;&lt;span class="n"&gt;three&lt;/span&gt;&lt;span class="s"&gt;', 4

# Accessing items in a tuple
print(myTuple[0])        #'&lt;/span&gt;&lt;span class="n"&gt;one&lt;/span&gt;&lt;span class="s"&gt;'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Sets
&lt;/h2&gt;

&lt;p&gt;A set allows you to store only unique values in the collection. Python  sets are &lt;strong&gt;unordered&lt;/strong&gt;. Therefore, elements in a set cannot&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;# Creating an empty set
&lt;/span&gt;&lt;span class="n"&gt;mySet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Creating a set with values
&lt;/span&gt;&lt;span class="n"&gt;myOtherSet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'one'&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;'three'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Computer science provides definition for more data structures than the built into python&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Stacks
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EksJ9BRE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/he7eb8sfrijzl3gvmrk9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EksJ9BRE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/he7eb8sfrijzl3gvmrk9.png" alt="Visual representation of a stack" width="814" height="482"&gt;&lt;/a&gt;&lt;br&gt;
A stack is a data structures that allows you to access the last and only the last item that was inserted into the data structure. You can envision a stack as a &lt;em&gt;bucket&lt;/em&gt;. You insert items into a bucket from the top one by one, but when it comes to removing items from the bucket, you remove them starting by the one that was last inserted all the way to the first. This is also known &lt;strong&gt;LIFO&lt;/strong&gt; (Last In First Out) principle. Inserting an item into a stack is known as &lt;strong&gt;pushing&lt;/strong&gt; the stack, and removing an item from the stack is known as &lt;strong&gt;popping&lt;/strong&gt; the stack. Accessing the last inserted item in a stack without necessarily removing it from the stack is known as &lt;strong&gt;peeking&lt;/strong&gt; the stack.&lt;br&gt;
Since the stack data structure is not built into Python, a popular implementation involves a class wrapper around the list data structure, with methods to such as &lt;code&gt;pop()&lt;/code&gt;, &lt;code&gt;push()&lt;/code&gt; and &lt;code&gt;peek()&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;class&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="err"&gt;”””&lt;/span&gt;&lt;span class="n"&gt;LIFO&lt;/span&gt; &lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;Python&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;underlying&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&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="err"&gt;”””&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;empty&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;”””&lt;/span&gt;
        &lt;span class="c1"&gt;# nonpublic list instance
&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;data&lt;/span&gt; &lt;span class="o"&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;len&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="err"&gt;”””&lt;/span&gt;&lt;span class="n"&gt;Return&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;elements&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;”””&lt;/span&gt; 
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;len&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;data&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;is_empty&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="err"&gt;”””&lt;/span&gt;&lt;span class="n"&gt;Return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;”””&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;len&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;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;push&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;e&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="err"&gt;”””&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;”””&lt;/span&gt;
        &lt;span class="c1"&gt;# new item stored at end of list
&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;data&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;e&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;peek&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="err"&gt;”””&lt;/span&gt;&lt;span class="n"&gt;Return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;but&lt;/span&gt; &lt;span class="n"&gt;do&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;remove&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;element&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="n"&gt;Raise&lt;/span&gt; &lt;span class="n"&gt;Empty&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="err"&gt;”””&lt;/span&gt;
        &lt;span class="k"&gt;if&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;is_empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;empty&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;# the last item in the list
&lt;/span&gt;        &lt;span class="k"&gt;return&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;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;−&lt;/span&gt;&lt;span class="mi"&gt;1&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;pop&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="err"&gt;”””&lt;/span&gt;&lt;span class="n"&gt;Remove&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;stack&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="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.,&lt;/span&gt; &lt;span class="n"&gt;LIFO&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;
        &lt;span class="n"&gt;Raise&lt;/span&gt; &lt;span class="n"&gt;Empty&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="err"&gt;”””&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;empty&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;# remove last item from list
&lt;/span&gt;        &lt;span class="k"&gt;return&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;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Queues
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9jXwMxiv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcxyibi01opt6sqvb11s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9jXwMxiv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcxyibi01opt6sqvb11s.png" alt="visual representation of a queue" width="880" height="137"&gt;&lt;/a&gt;&lt;br&gt;
A queue is a data structure that kind of works like a &lt;em&gt;line or people waiting to be served&lt;/em&gt;. You are free to insert as many items into a queue as you want, but you can only remove them starting from the the first item that was inserted, one by one, all the way to the last item that was inserted. Note that this the complete opposite of the workings of the stack data structure. Such a principle is known as &lt;strong&gt;FIFO&lt;/strong&gt; (First In First Out). Inserting items into a queue is known as &lt;strong&gt;enqueuing&lt;/strong&gt;, and removing items from a queue is known as &lt;strong&gt;dequeuing&lt;/strong&gt;. &lt;br&gt;
Although one can implement a queue as a wrapper class around the list data structure with &lt;code&gt;enqueue()&lt;/code&gt; and &lt;code&gt;dequeue()&lt;/code&gt; methods, this approach is generally advised against. The dynamic nature of Python's lists makes them inefficient for this task.&lt;br&gt;
Instead, use the &lt;code&gt;deque&lt;/code&gt; (pronounced as deck) class from Python's &lt;code&gt;collections&lt;/code&gt; module to create queues in your programs. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that deques are &lt;em&gt;double-ended queues&lt;/em&gt;, allowing you to access items from both ends of the queue. If this is not the kind of behavior you want from your queue, you can use the &lt;code&gt;Queue&lt;/code&gt; class from the &lt;code&gt;queue&lt;/code&gt; module&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&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="nn"&gt;collections&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;deque&lt;/span&gt;
&lt;span class="n"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s"&gt;"Eric"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Michael"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;queue&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;"Terry"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;           &lt;span class="c1"&gt;# Terry arrives
&lt;/span&gt;&lt;span class="n"&gt;queue&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;"Graham"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# Graham arrives
&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;popleft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;                 &lt;span class="c1"&gt;# The first to arrive now leaves (Eric)
&lt;/span&gt;&lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;popleft&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;                 &lt;span class="c1"&gt;# The second to arrive now leaves(John)
&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;queue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                    &lt;span class="c1"&gt;# Remaining queue in order of arrival
#(['Michael', 'Terry', 'Graham'])
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Introduction to modern Python</title>
      <dc:creator>Paul Nasdaq</dc:creator>
      <pubDate>Mon, 14 Feb 2022 09:19:50 +0000</pubDate>
      <link>https://dev.to/nasdaqpaul/introduction-to-modern-python-23bl</link>
      <guid>https://dev.to/nasdaqpaul/introduction-to-modern-python-23bl</guid>
      <description>&lt;h1&gt;
  
  
  Wait, wait.... So what really is Python?
&lt;/h1&gt;

&lt;p&gt;Python is a popular high level general purpose programming language. It has garnered reputation for being an elegant and easy to use language,(compared to other popular languages like C, C++ and Java) with clean and easy to read source code and easy to use data structures. This makes it ideal for newcomers to the world of programming and the perfect language for people who want to get a program up and running real quick but don't really want to get deep into software engineering territories. (e.g Data scientist, script writers, automation etc). Python is interpreted and supports multiple programming paradigms. Because of all this, Python has found successful uses in various fields of computing such as&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Data science&lt;/li&gt;
&lt;li&gt;Software engineering&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Python : The good parts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. It is dynamically typed
&lt;/h3&gt;

&lt;p&gt;Dynamic languages are easier to work with. You do not have to explicitly specify the data types of variables in your program. This makes your source code more concise and less verbose. You also write less code, and this translates to increased productivity. You do not have perform any explicit type casting, conversion or coercion. (Some type conversions can result in loss of precision, and others are just not possible).&lt;/p&gt;

&lt;h3&gt;
  
  
  2. It it multi-paradigm
&lt;/h3&gt;

&lt;p&gt;Python allows you to write your program in pretty much any style you may desire. A language like C is strictly procedural. You break down the programming tasks into functions (procedures) and call those functions to complete your programming task. A language like Java is strictly Object-Oriented. Everything has to be defined in a class. Python on the other hand allows you to mix these styles, and supports others, such as functional and asynchronous programming.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. It has a rich standard library
&lt;/h3&gt;

&lt;p&gt;The Python's standard library is massive, arguably one of the largest. It comes with packages and modules for some of the most common programming tasks such as network I/O,file manipulation, data persistence, data compression, cryptography, concurrency etc, out of the box. Furthermore, more specialized packages can be downloaded from the internet (usually for free) through PIP, Python's easy to use package manager.&lt;/p&gt;

&lt;h2&gt;
  
  
  Python: The bad parts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. It is slow
&lt;/h3&gt;

&lt;p&gt;It is safe to say that Python is slower on average compared to other popular programming languages on the market today. This is actually expected, due to a number reasons. It is interpreted, meaning that it cannot be optimized for a particular CPU architecture like compiled languages can. Plus there is a small overhead of running an interpreter below your code. Pretty much all primitive data types are immutable in Python. As such, modifying such values once created is usually a very expensive operation. Compiled and statically typed languages directly manipulate the bytes in a computer memory when they need to modify a variable, and this is magnitudes faster.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. It is not the most memory efficient language
&lt;/h3&gt;

&lt;p&gt;It is not uncommon for a Python variable to occupy way more memory than is ever going to be used during runtime. In comparison, a language like Go allow a programmer to specify how much memory a variable will occupy. For example, through constructs such as &lt;code&gt;int8&lt;/code&gt;, &lt;code&gt;int16&lt;/code&gt;, &lt;code&gt;int32&lt;/code&gt; and &lt;code&gt;int64&lt;/code&gt;, a programmer can explicitly state that an integer variable should take 8 bits, 16 bits, 32 bits and 64 bits of memory respectively. This level of control allows a programmer to craft arguably more efficient programs.&lt;br&gt;
This is also the reason (if you asked me) as to why Python is not gaining much traction in mobile computing and embedded systems, as these platforms are relatively resource constraint&lt;/p&gt;

&lt;h3&gt;
  
  
  3. It is dynamically typed
&lt;/h3&gt;

&lt;p&gt;Dynamic typing as a feature of a language is a double edged sword. On one hand it allows you to quickly get up and running with programming, and on the other hand it becomes a problem in really large software projects. It is possible to assign to a variable a value with a type that should &lt;strong&gt;NEVER&lt;/strong&gt; be assigned to said variable. A hypothetical &lt;code&gt;age&lt;/code&gt; variable in a program can be assigned a &lt;code&gt;string&lt;/code&gt; value, and this will go undetected until runtime. Static types are also self documenting. From a function's signature, you can quickly know what types of values a function/method expects and what type of values it returns (if any). You won't have to explicitly document this, for example, using Python's docstrings.&lt;br&gt;
The good news is that Python provides built-in support for type annotations through it's &lt;code&gt;typing&lt;/code&gt; module. This allows you to annotate the data types of variables and arguments that a function/method takes. It is therefore a good idea to use this in really large Python projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  The take away
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;All programming languages are nothing but tools.&lt;br&gt;
&lt;em&gt;Said pretty much any experienced programmer out there&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And in this regard, Python too, is nothing else but a tool. Like any tool out there, it is specifically designed to be really good at some tasks, and not so good at others. It probably isn't a good idea to use a screwdriver to drive nails into wood(even though, with enough determination, you can)&lt;br&gt;
It is therefore wise, as a programmer, to select and use languages that are fit for particular problems.&lt;br&gt;
Scripting, prototyping and small scale to medium scale software projects.. Python is a good bet.&lt;br&gt;
Large enterprise applications, applications with need for speed, resource starved platforms.... I'd be more than happy to switch.&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
