How about:
def is_prime(n):
""" Check if n is a prime number
@params:
:n: integer number
@returns: Boolean, True if n is prime number
else False
"""
return all(n%div and n%(div+2) for div in xrange(2, int(n**0.5)+1))
This gains a little performance:
import timeit
def isprime(n):
return len(filter(
lambda x:x==0,
map(
lambda y: n%y,range(2, int(n**0.5)+1)
)
))==0
def is_prime(n):
""" Check if n is a prime number
@params:
:n: integer number
@returns: Boolean, True if n is prime number
else False
"""
return all(n%div and n%(div+2) for div in xrange(2, int(n**0.5)+1))
print timeit.timeit('[is_prime(n) for n in xrange(100)]', 'from __main__ import is_prime', number=10000)
print timeit.timeit('[isprime(n) for n in xrange(100)]', 'from __main__ import isprime', number=10000)
Run it:
$ python test.py
2.07219815254
3.40691709518