Write a Python program to find the first n prime numbers
Write a Python program to find the first n prime numbers
In this article we are going to discuss the concept of “Write a Python program to find the first n prime numbers”
Prime numbers are fundamental in mathematics, appearing in numerous applications from cryptography to number theory. They are integers greater than 1, divisible only by 1, and themselves.

In this article, we’ll explore how to write a Python program to compute the first.
Program 1
# Write a Python program to find the first n prime numbers n = int(input("Enter the number of prime numbers to find: ")) if n <= 0: print("Please enter a positive integer.") #input a positive integer from user else: primes = [] num = 2 # Start checking for primes from 2 while len(primes) < n: is_prime = True for i in range(2, int(num**0.5) + 1): if num % i == 0: is_prime = False break if is_prime: primes.append(num) num += 1 print(f"The first {n} prime numbers are: {primes}")
When the above code is executed, it produces the following result
Enter the number of prime numbers to find: 10
The first 10 prime numbers are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Explanation:
- The program starts by asking the user for n, the number of primes to find.
- It initializes an empty list of primes to store the prime numbers and a variable “num” set to 2 (the first prime number).
- A while loop runs until the list primes contain n elements.
- Inside the loop:
- It checks if the current “num” variable is prime by iterating from 2 to the square root of the “num” variable.
- If no divisors are found, the “num” is added to the list of primes.
- The “num” is incremented and the process repeats.
- Finally, the program prints the list of the first n prime numbers.
Program 2
# Write a python program to find first n prime numbers def is_prime(num): if num<2: return False for i in range(2, int(num**0.5) + 1): if num%i==0: return False return True def first_n_primes(n): primes=[] num=2 #start cheking from 2 , the smallest prime while len(primes)<n: if is_prime(num): primes.append(num) num+=1 return primes #input the number opf primes to find n=int(input("Enter the number to find prime numbers: ")) print(f"The first {n} prime numbers are: {first_n_primes(n)}")
When the above code is executed, it produces the following result
Enter the number to find prime numbers: 6
The first 6 prime numbers are: [2, 3, 5, 7, 11, 13]
- is_prime(num): In this program, this function is used to determine if a number is a prime by checking divisibility up to the square root of the number
- first_n_primes(n): In this program’s function, a “while loop” is used to keep finding primes until the desired count is reached.
- The user inputs the number to n, and the program outputs the first n prime numbers
Suggested for you
Program to find whether a Number is Prime or Not in C++
Program to find whether a Number is Prime or Not in C
Program to find whether a Number is Prime or Not in Java
Program to find whether a Number is Prime or Not in Python
Program to find whether a Number is Prime or Not in PHP
Program to find whether a Number is Prime or Not using the PHP function
C++ program to find first n prime numbers
Java program to find first n prime numbers
C program to find first n prime numbers
C code to 5 ways to check whether the given integer is Even or Odd
Java code to 5 ways to check whether the given integer is Even or Odd
C++ code to 5 ways to check whether the given integer is Even or Odd
Python code to 5 ways to check whether the given integer is Even or Odd