<?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: prakx1</title>
    <description>The latest articles on DEV Community by prakx1 (@prakx1).</description>
    <link>https://dev.to/prakx1</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%2F267278%2F2496dc46-0dec-4637-bc13-a4d5b34b7142.jpeg</url>
      <title>DEV Community: prakx1</title>
      <link>https://dev.to/prakx1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prakx1"/>
    <language>en</language>
    <item>
      <title>Python collections, Part -I</title>
      <dc:creator>prakx1</dc:creator>
      <pubDate>Thu, 24 Dec 2020 06:09:12 +0000</pubDate>
      <link>https://dev.to/prakx1/python-collections-part-i-3b6e</link>
      <guid>https://dev.to/prakx1/python-collections-part-i-3b6e</guid>
      <description>&lt;p&gt;&lt;em&gt;Python always surprises and this time it made my work a lot easier so I am writing this&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Python collections are container datatypes.So, what are they?&lt;strong&gt;Container datatypes&lt;/strong&gt; are data structures that store other objects(integers,string,list user-defined objects) and also provide a way to access and traverse them. &lt;/p&gt;

&lt;p&gt;Python provides some of these container datatypes in &lt;a href="https://docs.python.org/3/library/collections"&gt;collections module&lt;/a&gt; to make it easier to store and access items.In this first part we will see &lt;strong&gt;counter&lt;/strong&gt;.&lt;br&gt;
It returns a dictionary with the keys as the elements of the object with the count of the element  as the value.&lt;br&gt;
Let's see an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; from collections import Counter
&amp;gt;&amp;gt;&amp;gt; c = Counter("ABCDABC")
&amp;gt;&amp;gt;&amp;gt; c #returns the count of each letter in string
Counter({'A': 2, 'B': 2, 'C': 2, 'D': 1})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; from collections import Counter
&amp;gt;&amp;gt;&amp;gt; c = Counter(['python','collections','are','interesting'])
&amp;gt;&amp;gt;&amp;gt; c #returns the counts of each element in list
Counter({'python': 1, 'collections': 1, 'are': 1, 'interesting': 1})
&amp;gt;&amp;gt;&amp;gt; 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Getting values from counter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; c['collections']
1 #collecctions appeared only once
&amp;gt;&amp;gt;&amp;gt; c['interesting']
1 #intersting appeared only  once  
&amp;gt;&amp;gt;&amp;gt; c['movie']
0 # 'movie' is not present so the output is 0
&amp;gt;&amp;gt;&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: Trying to get values not present in counter  return 0.&lt;br&gt;
&lt;em&gt;For more&lt;/em&gt;&lt;br&gt;
&lt;em&gt;References&lt;/em&gt;:&lt;br&gt;
&lt;a href="https://docs.python.org/3/library/collections.html#collections.Counter"&gt;Python documentation&lt;/a&gt;&lt;br&gt;
&lt;a href="https://en.wikipedia.org/wiki/Container_(abstract_data_type)"&gt;Container&lt;/a&gt; &lt;br&gt;
&lt;strong&gt;Thanks and merry Christmas&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://i.giphy.com/media/euviDNZfscxPOVSkHa/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/euviDNZfscxPOVSkHa/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is Duck typing in Python</title>
      <dc:creator>prakx1</dc:creator>
      <pubDate>Thu, 19 Nov 2020 12:42:11 +0000</pubDate>
      <link>https://dev.to/prakx1/what-is-duck-typing-in-python-4f85</link>
      <guid>https://dev.to/prakx1/what-is-duck-typing-in-python-4f85</guid>
      <description>&lt;p&gt;Duck typing got it's name from &lt;a href="https://en.wikipedia.org/wiki/Duck_test"&gt;Duck test&lt;/a&gt;-&lt;em&gt;"If it walks like a duck and it quacks like a duck, then it must be a duck".&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In python (and other dynamically typed languages)it doesn't matter what is the type of object what matters is that what it do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Elephant:
    def fly(self):
        print("Elephant is flying")


class Duck:
    def fly(self):
        print("Duck is flying")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take the two objects elephant and duck with same method &lt;code&gt;fly&lt;/code&gt;.Let us create a function &lt;code&gt;animal_flying&lt;/code&gt; that makes the animal fly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def animal_flying(animal):
    animal.fly()
animal_flying(Duck())
animal_flying(Elephant())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;[Output]:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Elephant is flying
Duck is flying
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function makes both duck and elephant fly without checking there type as both have the fly method, and this feature of using a object depending upon the methods it has irrespective of its type  is called &lt;strong&gt;Duck typing&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>datascience</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
