I have spent over the years writing Python code for everything from simple automation scripts to complex data pipelines.
One task that always comes up in technical interviews and classroom assignments is printing prime numbers within a specific range.
In this tutorial, I will show you how to print prime numbers from 1 to N using four different approaches.
What is a Prime Number in Python?
A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. For example, the numbers 2, 3, 5, 7, 11, and 13 are prime numbers because they have no divisors other than 1 and themselves.
Print Prime Numbers from 1 to N in Python
Now, let me show you how to print prime numbers from 1 to n in Python using various methods with examples.
Method 1: Basic Iteration and Checking
The simplest way to find and print prime numbers from 1 to N in Python is by using basic iteration and checking for each number’s divisibility.
Here is the complete Python code to print prime numbers from 1 to n in Python.
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
def print_primes(n):
for num in range(2, n + 1):
if is_prime(num):
print(num)
# Example usage
N = 50
print_primes(N)I executed the above example code and added the screenshot below.

In this example, the is_prime function checks if a number is prime by testing divisibility from 2 up to the square root of the number. The print_primes function iterates from 2 to N and prints the prime numbers.
Method 2: Use the Sieve of Eratosthenes
The Sieve of Eratosthenes is an efficient algorithm to find all primes up to a given limit. It works by iteratively marking the multiples of each prime starting from 2.
Here is a Python program to print prime numbers from 1 to n.
def sieve_of_eratosthenes(n):
primes = [True] * (n + 1)
p = 2
while p**2 <= n:
if primes[p]:
for i in range(p**2, n + 1, p):
primes[i] = False
p += 1
for p in range(2, n + 1):
if primes[p]:
print(p)
# Example usage
N = 50
sieve_of_eratosthenes(N)I executed the above example code and added the screenshot below.

In this example, we initialize a list of boolean values representing the primality of each number. We then mark the multiples of each prime number as False. Finally, we print the numbers that remain True.
Method 3: Use Python’s Built-In Libraries
Python’s sympy library includes a function to generate prime numbers. This method is straightforward and leverages the power of existing libraries.
Here is a prime number program in Python.
from sympy import primerange
def print_primes(n):
primes = list(primerange(1, n + 1))
for prime in primes:
print(prime)
# Example usage
N = 50
print_primes(N)In this example, we use primerange from the sympy library to generate a list of prime numbers up to N and then print them.
Print the First 10 Prime Numbers in Python Using a While Loop
Here, let me show you two methods to print the first 10 prime numbers using a while loop in Python.
Method 1: Basic While Loop with Prime Check Function
This method uses a while loop to iterate through numbers and a helper function to check if a number is prime.
Here is a complete example to print the first 10 prime numbers in Python using a while loop.
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
def print_first_10_primes():
count = 0
num = 2
while count < 10:
if is_prime(num):
print(num)
count += 1
num += 1
# Example usage
print_first_10_primes()I executed the above example code and added the screenshot below.

- Prime Check Function (is_prime): This function checks if a number is prime by testing its divisibility from 2 up to the square root of the number.
- Main Function (print_first_10_primes): This function uses a while loop to find and print the first 10 prime numbers. It starts with num = 2 and increments num until 10 prime numbers are found.
Method 2: Optimized While Loop with Early Termination
This method optimizes the prime-checking process by adding an early-termination condition to the helper function.
Here is a complete example of printing the first n prime numbers in Python using a while loop.
def is_prime(num):
if num <= 1:
return False
if num == 2:
return True
if num % 2 == 0:
return False
for i in range(3, int(num**0.5) + 1, 2):
if num % i == 0:
return False
return True
def print_first_10_primes():
count = 0
num = 2
while count < 10:
if is_prime(num):
print(num)
count += 1
num += 1
# Example usage
print_first_10_primes()- Optimized Prime Check Function (is_prime): This function includes additional checks:
- It returns False for numbers less than or equal to 1.
- It returns True for the number 2.
- It returns False for even numbers greater than 2.
- It checks divisibility only for odd numbers starting from 3 up to the square root of the number.
- Main Function (print_first_10_primes): Similar to the first method, this function uses a while loop to find and print the first 10 prime numbers.
Write a Python Program to Print Prime Numbers Less Than 20
Now, let me give you another example. Here, I will show you how to write a Python program to print prime numbers less than 20.
I will show you how to do this using basic iteration with the prime check function.
This method involves iterating through numbers less than 20 and using a helper function to check if each number is prime.
Here is the complete Python code and an example.
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
def print_primes_less_than_20():
for num in range(2, 20):
if is_prime(num):
print(num)
# Example usage
print_primes_less_than_20()- Prime Check Function (
is_prime): This function checks if a number is prime by testing its divisibility from 2 up to the square root of the number.- If the number is less than or equal to 1, it returns
False. - It iterates from 2 to the square root of the number, checking if the number is divisible by any of these values. If it is, the function returns
False. - If the number is not divisible by any of these values, the function returns
True.
- If the number is less than or equal to 1, it returns
- Main Function (print_primes_less_than_20): This function uses a for loop to iterate through numbers from 2 to 19 (since we want primes less than 20).
- For each number, it calls the
is_primefunction to check if the number is prime. - If the number is prime, it prints the number.
- For each number, it calls the
Output:
When you run the program, it will print the following prime numbers less than 20:
2
3
5
7
11
13
17
19I executed the above example code and added the screenshot below.

Print the First 10 Prime Numbers in Python Using a For Loop
Let me now show you two methods to print the first 10 prime numbers using a for loop in Python.
Method 1: Basic For Loop with Prime Check Function
This method uses a for loop to iterate through numbers and a helper function to check if a number is prime in Python.
Here is a complete example.
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
def print_first_10_primes():
count = 0
num = 2
while count < 10:
if is_prime(num):
print(num)
count += 1
num += 1
# Example usage
print_first_10_primes()- Prime Check Function (is_prime): This function checks if a number is prime by testing its divisibility from 2 up to the square root of the number.
- Main Function (print_first_10_primes): This function uses a for loop to find and print the first 10 prime numbers. It starts with num = 2 and increments
numuntil 10 prime numbers are found.
I executed the above example code and added the screenshot below.

Method 2: Optimized For Loop with Early Termination
This method optimizes the prime-checking process by adding an early-termination condition to the helper function.
Here is another example of printing the first 10 prime numbers in Python.
def is_prime(num):
if num <= 1:
return False
if num == 2:
return True
if num % 2 == 0:
return False
for i in range(3, int(num**0.5) + 1, 2):
if num % i == 0:
return False
return True
def print_first_10_primes():
count = 0
num = 2
while count < 10:
if is_prime(num):
print(num)
count += 1
num += 1
# Example usage
print_first_10_primes()- Optimized Prime Check Function (is_prime): This function includes additional checks:
- It returns False for numbers less than or equal to 1.
- It returns True for the number 2.
- It returns False for even numbers greater than 2.
- It checks divisibility only for odd numbers starting from 3 up to the square root of the number.
- Main Function (print_first_10_primes): Similar to the first method, this function uses a for loop to find and print the first 10 prime numbers.
This is how to print the first 10 prime numbers using a for loop in Python.
I hope you found this tutorial helpful!
Using the right method depends entirely on the size of the number N you are working with. For small numbers, a simple loop is fine, but for larger tasks, I highly recommend the Sieve of Eratosthenes.
You may also like to read:
- Interfaces in Python
- Command Errored Out with Exit Status 1 in Python
- Priority Queue in Python
- Fibonacci Series Program in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.