Question: I came across this (really) simple program a while ago. It just outputs the first x primes. I'm embarrassed to ask, is there any way to make it more "pythonic" ie condense it while making it (more) readable? Switching functions is fine; I'm only interested in readability.

Thanks

```
from math import sqrt

def isprime(n):
  if n ==2:
    return True
  if n % 2 ==0 : # evens
    return False

  max = int(sqrt(n))+1 #only need to search up to sqrt n 
  i=3
  while i <= max: # range starts with 3 and for odd i
    if n % i == 0:
      return False
    i+=2

  return True

reqprimes = int(input('how many primes: '))
primessofar = 0
currentnumber = 2
while primessofar < reqprimes:

  result = isprime(currentnumber)

  if result:
     primessofar+=1
     print currentnumber
     #print '\n'

  currentnumber += 1

```

Answer: 
Firstly, you should not assign max to a variable as it is an inbuilt function used to find the maximum value from an iterable. Also, that entire section of code can instead be written as

```
for i in xrange(3, int(sqrt(n))+1, 2):
    if n%i==0: return False

```

Also, instead of defining a new variable result and putting the value returned by isprime into it, you can just directly do

```
if isprime(currentnumber):

```
Usually you don't use while loops for simple things like this. You rather create a range object and get the elements from there. So you could rewrite the first loop to this for example:

```

for i in range( 3, int( sqrt( n ) ) + 1, 2 ):
    if n % i == 0:
        return False

```

And it would be a lot better if you would cache your prime numbers and only check the previous prime numbers when checking a new number. You can save a lot time by that (and easily calculate larger prime numbers this way). Here is some code I wrote before to get all prime numbers up to `n` easily:

```

def primeNumbers ( end ):
    primes = []
    primes.append( 2 )

    for i in range( 3, end, 2 ):
        isPrime = True

        for j in primes:
            if i % j == 0:
                isPrime = False
                break

        if isPrime:
            primes.append( i )

    return primes

print primeNumbers( 20 )

```