title: What does yield do in python 2.7?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/231767/the-python-yield-keyword-explained">The Python yield keyword explained</a>  </p>
  </blockquote>
  
  
  
  <p>Okay, I've probably phrased the question badly but this is the situation I have.</p>
  
  <p>I have this line of code in Python 2.7 which I'm trying to understand:</p>
  
  <pre><code>yield (padding_zeros + number_string).encode("ascii")
  </code></pre>
  
  <p>In this line of code, <code>padding_zeros</code> is a string of a variable number of '0's and <code>number_string</code> is a number in the form of a string which can be any number between 0 to, say 10000.</p>
  
  <p>I'm pretty confident that the <code>.encode("ascii")</code> just converts the output of yield to ascii.</p>
  
  <p>What I'm completely at sea about is what the <code>yield (padding_zeros + number_string)</code> does.</p>
  
  <p>I know it initiates a generator but I've spent a lot of time searching online and reading up on the syntax but I still can't work out what the generator actually <i>does</i>. It doesn't help that this is my first time looking at python (my ultimate aim is to convert this code to C#).</p>
  
  <p>So, basically, please could someone explain to me what this line of code does? Does it just add the two strings together or does it do something a bit more complicated?</p>
  
  <p>For further context, this is the block that that line of code appears in:</p>
  
  <pre><code>for current_length in range(4, max_length + 1):
      for i in range(0, pow(10, current_length)):
          number_string = str(i)
          padding_zeros = "0" * (current_length - len(number_string))
          yield (padding_zeros + number_string).encode("ascii")
  </code></pre>
  
  <p><i>(<code>max_length</code> being exactly what it sounds like - a number indicating the maximum length of something)</i></p>
  
  <p>Thanks in advance for any and all answers (even if they're telling me not to be such a fricking noob) :)</p>
  
  <p>EDIT: Thanks very much for the answers - even though I could only pick one as the best answe they were all very helpful. And thanks for the comments as well - as some of them pointed out, <a href="https://stackoverflow.com/questions/231767/the-python-yield-keyword-explained/231855#231855">What does the &quot;yield&quot; keyword do in Python?</a> is a very good general guide to yield, generators and iterations even if I didn't find it an answer to my specific situation :)</p>