Trying to understand Python loop using underscore and input
<p>One more tip - if anyone is learning Python on HackerRank, knowing this is critical for starting out.</p>

<p>I'm trying to understand this code:</p>

<pre><code>    stamps = set()
    for _ in range(int(raw_input())):
        print 'underscore is', _
        stamps.add(raw_input().strip())
        print stamps
</code></pre>

<p>Output:</p>

<pre><code>    &gt;&gt;&gt;2 
    underscore is 0
    &gt;&gt;&gt;first
    set(['first'])
    underscore is 1
    &gt;&gt;&gt;second
    set(['second', 'first'])
</code></pre>

<ol>
<li><p>I put 2 as the first raw input. How does the function know that I'm only looping twice? This is throwing me off because it isn't the typical...for i in xrange(0,2) structure. </p></li>
<li><p>At first my thinking was the underscore repeats the last command in shell. So I added print statements in the code to see the value of underscore...but the values just show the 0 and 1, like the typical loop structure.</p></li>
</ol>

<p>I read through this post already and I still can't understand which of those 3 usages of underscore is being used. </p>

<p><a href="http://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python">What is the purpose of the single underscore &quot;_&quot; variable in Python?</a></p>

<p>I'm just starting to learn Python so easy explanations would be much appreciated!</p>

<p>For anyone that is trying to understand how underscore and input works in a loop - after spending quite sometime debugging and printing - here's the code that made me understand what was going on.</p>

<pre><code>    for _ in range(int(raw_input())):
        print raw_input()
</code></pre>

<p>User input:</p>

<pre><code>    2
    Dog
    Cat
</code></pre>

<p>Output:</p>

<pre><code>    # no output despite entering 2, but 2 is set as range - loops 2 times
    Dog
    Cat
</code></pre>

<p>Bonus - notice how there's an int() conversion for the first line in the for loop? </p>

<p>The first input is 2, so int() converts that just fine. You can tell the first line of code is being ignored now because putting the second input, 'Dog', through int() would yield an error. Can't words into integer numbers.</p>

python loops input