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><a href="http://stackoverflow.com/a/5893946/918959">ngoghlan's answer</a> lists 3 conventional uses for <code>_</code> in Python:</p>

<blockquote>
  <ol>
  <li>To hold the result of the last executed statement in an interactive
  interpreter session. This precedent was set by the standard CPython
  interpreter, and other interpreters have followed suit</li>
  <li><p>For translation lookup in i18n (imported from the corresponding C
  conventions, I believe), as in code like: </p>

<pre><code>raise forms.ValidationError(_("Please enter a correct username"))`
</code></pre></li>
  <li><p>As a general purpose "throwaway" variable name to indicate that part
  of a function result is being deliberately ignored, as in code like:</p>

<pre><code> label, has_label, _ = text.partition(':')
</code></pre></li>
  </ol>
</blockquote>

<hr>

<p>Your question is which one of these is being used in the example in your code. The answer would be that is a throwaway variable (case 3), but its contents are printed <strong>here</strong> for debugging purposes.</p>

<p>It is however not a general Python convention to use <code>_</code> as a loop variable if its value is used in any way. Thus you regularly might see:</p>

<pre><code> for _ in range(10):
     print("Hello world")
</code></pre>

<p>where <code>_</code> immediately signals the reader that the value is not important and it the loop is just repeated 10 times. </p>

<p>However in a code such as</p>

<pre><code> for i in range(10):
     do_something(i)
</code></pre>

<p>where the value of the loop variable is used, it is the convention to use a variable name such as <code>i</code>, <code>j</code> instead of <code>_</code>.</p>

python loops input