Back to Python
2026-04-275 min read

Python Program to Print all Prime Numbers in an Interval

Learn Python Program to Print all Prime Numbers in an Interval step by step with clear examples and exercises.

Title: Python Program to Print all Prime Numbers in an Interval

Why This Matters

Prime numbers play a crucial role in mathematics and computer science due to their unique properties. They are essential for various applications such as cryptography, number theory, and algorithm analysis. In this lesson, we will learn how to write a Python program that can print all the prime numbers within a given interval, which is an essential skill for coding interviews or competitions.

Prerequisites

Before diving into the core concept, it's essential to have a good understanding of the following topics:

  • Basic Python syntax and data structures (variables, loops, functions)
  • Understanding of basic arithmetic operations and modulus operator
  • Familiarity with control flow statements like if and else
  • Knowledge of list comprehensions and built-in Python functions like range(), len(), math.sqrt(), and math.isqrt() (integer square root)

Core Concept

A prime number is a positive integer greater than 1 that has no other factors except 1 and itself. For example, 2, 3, 5, and 7 are all prime numbers. However, 6 is not a prime number because it can be divided by both 2 and 3.

To write a Python program to print all the prime numbers within an interval, we will use a simple approach that checks each number in the range for divisibility by other numbers between 2 and the square root of the number. If a number is not divisible by any number in this range, it's a prime number, and we print it.

Here's the Python code to achieve this:

import math

def is_prime(num):
if num <= 1:
return False
sqrt_num = math.isqrt(num)
for i in range(2, sqrt_num + 1):
if num % i == 0:
return False
return True

def print_primes(lower, upper):
primes = []
for num in range(lower, upper + 1):
if is_prime(num):
primes.append(num)
print("Prime numbers between", lower, "and", upper, "are:", primes)

Call the function with desired interval values

print_primes(900, 1000)


In this code:

- We define an `is_prime()` helper function to check if a given number is prime. It returns `True` if the number is prime and `False` otherwise.
- The `print_primes()` function takes two arguments: the lower and upper bounds of the interval.
- It initializes an empty list `primes` to store prime numbers found within the interval.
- The for loop iterates over every number in the range from the lower bound to one past the upper bound.
- If a number is determined to be prime by the `is_prime()` function, it's appended to the primes list.
- Finally, the function prints the list of prime numbers found within the specified interval.

Worked Example

Let's run the program for the interval between 900 and 1000:

print_primes(900, 1000)

Output:

Prime numbers between 900 and 1000 are: [907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]

Common Mistakes

  • Forgetting to check for divisibility by 2 separately: Since 2 is the smallest prime number, it should be checked before starting the loop over other numbers.
  • Not checking up to the square root of the number: To avoid unnecessary checks, only numbers less than or equal to the square root of the current number need to be tested for divisibility.
  • Missing edge cases: Make sure to handle the lower and upper bounds separately as they may or may not be prime numbers themselves.
  • Not defining the is_prime() helper function: It's essential to have a separate function to check if a number is prime, so we don't repeat the same code in multiple places.
  • Incorrectly calculating the square root of the number: Use the built-in math.isqrt() function for accurate integer square roots.

Practice Questions

  1. Write a Python program to find the sum of all prime numbers between 2 and 100.
  2. Modify the print_primes() function to print only the first 10 prime numbers found within the given interval.
  3. Write a Python program to check if a given number is prime or composite (not prime).
  4. Optimize the prime number checking algorithm by using list comprehensions and built-in Python functions.
  5. Implement the Sieve of Eratosthenes algorithm to find all prime numbers up to a specified limit.
  6. (Advanced) Implement an optimization technique that skips even numbers greater than 2 during the initial loop, as only odd numbers can be prime.
  7. (Advanced) Modify the print_primes() function to print the number of prime numbers found within the given interval along with the list of primes.
  8. (Challenge) Implement a Python program that generates all pairs of twin primes (pairs of prime numbers that differ by 2) up to a specified limit.

FAQ

How can I optimize the prime number checking algorithm?

One optimization technique is to skip even numbers greater than 2 during the initial loop, as only odd numbers can be prime. Another approach is to use Sieve of Eratosthenes, which eliminates multiples of each prime number as they are found, making subsequent checks faster.

Why do we need to check divisibility up to the square root of the number?

Since a composite number must have at least one factor greater than its square root, checking only numbers less than or equal to the square root ensures that all potential factors are considered without unnecessary checks on larger numbers.

How does the Sieve of Eratosthenes algorithm work?

The Sieve of Eratosthenes is an ancient algorithm used to find all prime numbers up to a specified limit. It works by iteratively marking the multiples of each prime number as composite, leaving only the remaining unmarked numbers as primes. The algorithm starts with all numbers marked as potential primes and then removes the multiples of the smallest unmarked number until no more remain. This process continues until all numbers up to the specified limit are either marked or removed.

Python Program to Print all Prime Numbers in an Interval | Python | XQA Learn