In Python, we can create a list of prime numbers easily – all we need is a custom function to check if a number is prime or not.
To generate a list of the first N prime numbers in Python, you can create your own function and loop until you have N prime numbers.
def isPrime(n):
if (n % 2 == 0):
return False
for i in range(3, int(n**0.5 + 1), 2):
if (n % i == 0):
return False
return True
def getFirstPrimes(n):
primes = [2]
num = 3
while (len(primes) < n):
if(isPrime(num)):
primes.append(num)
num = num + 2
return primes
print(getFirstPrimes(10))
#Output:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
If you want to create a list of prime numbers in a certain range, you can create your own function and loop over that range to find prime numbers.
def isPrime(n):
if (n % 2 == 0):
return False
for i in range(3, int(n**0.5 + 1), 2):
if (n % i == 0):
return False
return True
def getPrimesRange(a, b):
primes = []
if a > 1 and b > 1:
count = 0
if a > b:
t = a
a = b
b = t
if a == 2:
count = 1
a = 3
if a % 2 == 0:
a = a + 1
while (a < b):
if(isPrime(a)):
primes.append(a)
a = a + 2
return primes
else:
return "Not a valid range."
print(getPrimesRange(200,300))
#Output:
[211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]
When working with numbers in Python, the ability to create collections of specific numbers can be useful.
One such example is if you are working with prime numbers and want to create a list of prime numbers.
With Python, we can check if a number is prime or not with a simple user-defined Python isprime() function.
def isPrime(n):
if (n % 2 == 0):
return False
for i in range(3, int(n**0.5 + 1), 2):
if (n % i == 0):
return False
return True
Then, depending on the list you want to make, you can create a loop which will gather the primes in a list for you.
The rest of this article shows you two methods you can use to create prime number lists using Python.
Creating Prime Number List of First N Prime Numbers Using Python
One example of creating a list of primes is to create a list which has the first N prime numbers.
To generate a list of the first N prime numbers in Python, you can create your own function and loop until you have N prime numbers.
Below is an example of how you can get the first 10 prime numbers using Python.
def isPrime(n):
if (n % 2 == 0):
return False
for i in range(3, int(n**0.5 + 1), 2):
if (n % i == 0):
return False
return True
def getFirstPrimes(n):
primes = [2]
num = 3
while (len(primes) < n):
if(isPrime(num)):
primes.append(num)
num = num + 2
return primes
print(getFirstPrimes(10))
#Output:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Creating Prime Number List of Prime Numbers in Range Using Python
Another example where you might want to create a list of prime numbers is if you want only the prime numbers in a specific range.
If you want to create a list of prime numbers in a certain range, you can create your own function and loop over that range to find prime numbers.
Below is a function which you can use which will get the prime numbers in a range using Python.
def isPrime(n):
if (n % 2 == 0):
return False
for i in range(3, int(n**0.5 + 1), 2):
if (n % i == 0):
return False
return True
def getPrimesRange(a, b):
primes = []
if a > 1 and b > 1:
count = 0
if a > b:
t = a
a = b
b = t
if a == 2:
count = 1
a = 3
if a % 2 == 0:
a = a + 1
while (a < b):
if(isPrime(a)):
primes.append(a)
a = a + 2
return primes
else:
return "Not a valid range."
print(getPrimesRange(200,300))
#Output:
[211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]
Hopefully this article has been useful for you to learn how to get a list of prime numbers with Python.