title: Python program to store first N prime numbers in a list?
body: <p>I wrote the following code for finding N prime numbers. But,I have trouble       storing it in a list.</p>
  
  <pre><code>     def prime(n):
  
          global count
          s=0
          flag=0
          ls=[]
          for i in range(2,n/2+1):
                       if(n%i==0):
                             flag=1
                             break
  
          if(flag==0):
                  print n
                  ls.append(n)
                  count=count+1
          print ls
  
      # main pro
      n=input("Enter the number of prime numbers to be displayed: ")
      print"The prime numbers are:"  
      count=0
      x=2
      while(count&lt;n):
               prime(x)
               x+=1
  </code></pre>
  
  <p>My desired output should be:</p>
  
  <pre><code>    Enter the number of prime numbers to be displayed: 5
      [2, 3, 5, 7, 11]
  </code></pre>
  
  <p>But when I run the program,I get output as:</p>
  
  <pre><code>    Enter the number of prime numbers to be displayed: 5
      The prime numbers are:
      2
      [2]
      3
      [3]
      []
      5
      [5]
      []
      7
      [7]
      []
      []
      []
      11
      [11] 
  </code></pre>
  
  <p>How do I add prime numbers into a list without changing the above code??
  Should I append the elements at a later part? I am new to Python,so all help is appreciated! Thank you!</p>