While statement and expressions
<p><code>while</code> loop statement gets executed as long as the expression is true. </p>

<p>After the 10th time, the pop() throws an IndexError back because there are 10 elements in the list. But, the below code executes fine without any error. Why is it so?</p>

<pre><code>a = [x*x for x in range(10)]
while a.pop():
    print("elements in the list")
</code></pre>

<p>It might be a very basic question, but I am trying to get comfortable with Python.</p>

<p>It is unclear what your issue is. If I execute your code, it does (what I think) you expect. It is likely the <code>0</code> that is causing unexpected behavior:</p>

<pre><code>&gt;&gt;&gt; a = [x*x for x in range(10)]
&gt;&gt;&gt; while a.pop():
...    print a
... 
[0, 1, 4, 9, 16, 25, 36, 49, 64]
[0, 1, 4, 9, 16, 25, 36, 49]
[0, 1, 4, 9, 16, 25, 36]
[0, 1, 4, 9, 16, 25]
[0, 1, 4, 9, 16]
[0, 1, 4, 9]
[0, 1, 4]
[0, 1]
[0]
</code></pre>

<p>It is more <em>Pythonic</em> to test the list itself only rather than the result of <code>.pop()</code> since that will terminate the loop on any falsy item:</p>

<pre><code>&gt;&gt;&gt; li=[1,2,3,False,4,5]
&gt;&gt;&gt; while li.pop():
...    print li
... 
[1, 2, 3, False, 4]
[1, 2, 3, False]
</code></pre>

<p>You can see that the <code>while</code> loop is terminated as it encounters <code>False</code> from pop. </p>

<p>It would be more typical to do:</p>

<pre><code>&gt;&gt;&gt; li=[1,2,3,False,4,5]
&gt;&gt;&gt; while li:
...    e=li.pop()
...    print e, li
... 
5 [1, 2, 3, False, 4]
4 [1, 2, 3, False]
False [1, 2, 3]
3 [1, 2]
2 [1]
1 []
</code></pre>

<p>which terminates when the list becomes false.</p>

<p>Better still (in Python) to use a <code>for</code> loop which is non destructive to the list:</p>

<pre><code>&gt;&gt;&gt; li=[1,2,3,False,4,5]
&gt;&gt;&gt; for e in li:
...    print e, li
... 
1 [1, 2, 3, False, 4, 5]
2 [1, 2, 3, False, 4, 5]
3 [1, 2, 3, False, 4, 5]
False [1, 2, 3, False, 4, 5]
4 [1, 2, 3, False, 4, 5]
5 [1, 2, 3, False, 4, 5]
</code></pre>

python while-loop