infinite while loop in python when integer compared with range
<p>my while code:</p>

<pre><code>i=0
    a = range(100)
    while i &lt; range(100):
        print i
        i += 9
</code></pre>

<p>this goes into an infinite loop...may i know why?</p>

<p>is it because an integer is compared to the list?
but what happens when i becomes greater than 99?</p>

<p>shouldnt it come out of the while loop?</p>

<p>below code works fine as expected:</p>

<pre><code>i=0
        a = range(100)
        a_len = len(a)
        while i &lt; a_len:
            print i
            i += 9
</code></pre>

<p><code>range(100)</code> is a list of integers from 1 to 100 over which you are supposed to iterate. So, <code>len(range(100)</code> = 100. In python 2.x, a list is always greater than an integer. A very simple way to fix this problem is:</p>

<pre><code>i=0
while i &lt; 100: # instead of range(100)
    print i
    i += 9
</code></pre>

python python-2.7 while-loop