<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Primerlabs Blog]]></title><description><![CDATA[We create self-paced courses to help you learn Computer Science in-depth. ]]></description><link>https://devblog.primerlabs.io</link><generator>RSS for Node</generator><lastBuildDate>Tue, 09 Jun 2026 14:28:18 GMT</lastBuildDate><atom:link href="https://devblog.primerlabs.io/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Understanding the Iterator Protocol in Python]]></title><description><![CDATA[This is an excerpt from our  free Python course  on  Primer . 
Iterables &  Iterators are at the core of Python. Let's learn what are they and how to understand them better. 
Some definitions first:
Iterables: An object capable of returning its membe...]]></description><link>https://devblog.primerlabs.io/understanding-the-iterator-protocol-in-python</link><guid isPermaLink="true">https://devblog.primerlabs.io/understanding-the-iterator-protocol-in-python</guid><category><![CDATA[Python 3]]></category><category><![CDATA[Python]]></category><dc:creator><![CDATA[Siddharth Kanungo]]></dc:creator><pubDate>Mon, 01 Nov 2021 11:22:07 GMT</pubDate><content:encoded><![CDATA[<p>This is an excerpt from our  <a target="_blank" href="https://primerlabs.io/books/python-i/">free Python course</a>  on  <a target="_blank" href="https://primerlabs.io">Primer</a> . </p>
<p>Iterables &amp;  Iterators are at the core of Python. Let's learn what are they and how to understand them better. </p>
<p>Some definitions first:</p>
<p><strong>Iterables</strong>: An object capable of returning its member one at a time is called an iter-able.</p>
<p>To get a member from an iterable, one at a time, Python provides a built-in function <code>iter()</code>. When we provide an iterable as an argument to the <code>iter()</code> function ( which in returns call the <code>__iter__()</code> <strong>dunder method</strong> on the object ), it returns something called <strong>iterator</strong>.</p>
<p>Let's check out in code:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>guests = {<span class="hljs-string">'Luffy'</span>, <span class="hljs-string">'Zorro'</span>, <span class="hljs-string">'Sanji'</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>iter(guests)
&lt;set_iterator object at <span class="hljs-number">0x7f7983d1dab0</span>&gt;
</code></pre>
<p>The above code listing provides the guests set to the iter() returns a set_iterator object. But what's an iterator?</p>
<ul>
<li>An iterator is an object representing a stream of data.
When you repeatedly call the built-in <code>next()</code> on the iterator object, it returns the stream's next items.</li>
<li>When no more items are available in the stream of data, the StopIteration exception is raised instead.</li>
</ul>
<p>To understand this, let's continue our code from above.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>guests = {<span class="hljs-string">'Luffy'</span>, <span class="hljs-string">'Zorro'</span>, <span class="hljs-string">'Sanji'</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>guest_iterator = iter(guests)
<span class="hljs-meta">&gt;&gt;&gt; </span>next(guest_iterator)
<span class="hljs-string">'Zorro'</span>                        <span class="hljs-comment"># Sets are unordered</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>next(guest_iterator)
<span class="hljs-string">'Luffy'</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>next(guest_iterator)
<span class="hljs-string">'Sanji'</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>next(guest_iterator)    <span class="hljs-comment"># No more items left in the set</span>
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
StopIteration
</code></pre>
<p>In the above code listing, we can see how the iterator returned from guests can provide one object at a time using built-in <code>iter()</code> and <code>next()</code> functions.</p>
<p>For an object to be iterable, it needs to have the dunder method, <code>__iter__()</code> defined. It's the <code>__iter__()</code> method, which returns an iterator from an iterable.</p>
<p>he built-in objects such as <code>sequences</code>, <code>sets</code>, and <code>dictionaries</code> have the dunder method <code>__iter__()</code> defined, which lets us use in <code>for</code> loop.</p>
<p>Now that we have gained a bit of insight into how Python implements iterations let's try to write a <code>while</code> loop to iterate over a set of objects.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>guests = {<span class="hljs-string">'Luffy'</span>, <span class="hljs-string">'Zorro'</span>, <span class="hljs-string">'Sanji'</span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>guest_iterator = iter(guests)    <span class="hljs-comment"># Same as guests.__iter__()</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:
<span class="hljs-meta">... </span>    <span class="hljs-keyword">try</span>:
            <span class="hljs-comment"># Same as guest_iterator.__next__()</span>
<span class="hljs-meta">... </span>        guest = next(guest_iterator)
<span class="hljs-meta">... </span>        print(guest)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">except</span> StopIteration <span class="hljs-keyword">as</span> e:
<span class="hljs-meta">... </span>        <span class="hljs-keyword">break</span>
Zorro
Luffy
Sanji
</code></pre>
<p>The <code>while</code> loop we wrote is pretty close to what happens under the hood when we iterate on an iterable using a <code>for</code> loop. Although, when using iterables with the for loop, we don't need to call the <code>iter()</code> function or handle the <code>StopIteration</code> error as the for statement does that automatically for us.</p>
<p>We can describe the iterator protocol as the following:</p>
<ul>
<li>First, obtain the iterator of the object using the <code>iter()</code> function or the dunder method <code>&lt;iterable&gt;.__iter__()</code>.
Call the built-in function <code>next()</code> or the dunder method <code>&lt;iterator&gt;.__next__()</code> on the iterator object.</li>
<li>Run the code block inside the <code>for</code> block</li>
<li>Repeat the next invocation until the iterator raises <code>StopIteration</code></li>
</ul>
<p><img src="https://assets.primerlabs.io/CourseAssets/Python1/Chapter6/Iterators%2C+Generators+and+Comprehensions+-+Iterator+Protocol.svg" alt="Iterators,+Generators+and+Comprehensions+-+Iterator+Protocol.svg" /></p>
<p>Let's redefine our redefine the terms iterable and iterator.</p>
<p><strong>Iterable</strong>: An object that allows the iteration is called an iter-able. The iterable is required to have an <code>__iter__()</code> method defined that returns an iterator.</p>
<p><strong>Iterator</strong>: The iterator is required to have both an <code>__iter__()</code> method and a <code>__next__()</code> method.</p>
<p>So far, we can summarise everything we have learned about iterables and iterators in the table below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1635765435289/CWj5KKr7E.png" alt="Chapter-5-Iterators-Generators-Comprehensions (1).png" /></p>
<p>In this section, we covered the difference between iterators and iterables. If you wish to learn Python in-depth, you can visit  <a target="_blank" href="https://primerlabs.io">Primer</a>  and check out our free course on Python. You can also follow me on  <a target="_blank" href="https://primerlabs.io">twitter</a> . </p>
]]></content:encoded></item><item><title><![CDATA[Introducing Primer - Conversational Learning Medium]]></title><description><![CDATA[We at Primerlabs have created a new learning platform, designed specifically for self-learners. This comics-based blog post is our attempt at demystifying what Primer is and how is it helpful. 
This experimental blog post was originally posted at Pri...]]></description><link>https://devblog.primerlabs.io/introducing-primer-conversational-learning-medium</link><guid isPermaLink="true">https://devblog.primerlabs.io/introducing-primer-conversational-learning-medium</guid><category><![CDATA[learning]]></category><category><![CDATA[Computer Science]]></category><category><![CDATA[coding]]></category><category><![CDATA[Productivity]]></category><dc:creator><![CDATA[Siddharth Kanungo]]></dc:creator><pubDate>Wed, 21 Jul 2021 11:57:45 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484654462/y-c1hrsHd.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>We at <a target="_blank" href="https://primerlabs.io">Primerlabs</a> have created a new learning platform, designed specifically for self-learners. This comics-based blog post is our attempt at demystifying what Primer is and how is it helpful. </p>
<p>This experimental blog post was originally posted at <a target="_blank" href="https://primerlabs.io/comics/introducing-primer-comics/">Primer Blog</a>. You can view the post there as hashnode doesn't support embedding direct videos. </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632483462200/VwrIvkO0TQ.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632483467062/lQuRRY79R.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632483473067/Vp_wgVr61.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632483477142/8f4DGcMzv.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632483481711/Ao2_OrlFS.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632483486768/s-hAQXoVD.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484116200/KKo1rkjLb.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484144388/enYq_mlI8.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484218504/EI1_op19c.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484161804/2hwe73dMx.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484256744/2GlVd1zI1.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484266559/ZV_BFK9PL.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484299941/IIZ_jE5AQ.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484305047/U03CymTdH.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484335507/B0VvLYMJ0.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484343495/y0zHWuUbU.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484398769/611bfXXn0.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484406579/Tks3E8_JS.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484438118/9sbQ8gkGw.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484444247/L3MXN7Zev.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484450181/rjvNsMB-l.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484473074/5orBIqaYL.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484478279/bB-YMmBdX.png" alt="image.png" /></p>
<h1 id="primer-your-learning-companion-for-life">Primer: Your Learning Companion for Life</h1>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484507118/xYXIsSWsb.png" alt="image.png" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632484513999/YzuYz0HCQ.png" alt="image.png" /></p>
]]></content:encoded></item></channel></rss>