Skip to content
Devtoolry

Numbers · intermediate

Python Program to Print All Prime Numbers in an Interval

Print all prime numbers between two numbers in Python, first with a nested loop and for-else, then with the much faster Sieve of Eratosthenes.

Printing the primes in an interval, such as 10 to 50, combines a loop over the candidates with a second loop that looks for divisors. It is also the classic place to meet Python's for-else: the else block of a loop runs only when the loop was not stopped by break.

The program

Python
low, high = 10, 50

for n in range(max(low, 2), high + 1):
    for d in range(2, int(n ** 0.5) + 1):
        if n % d == 0:
            break
    else:
        print(n, end=" ")
print()
Output
11 13 17 19 23 29 31 37 41 43 47 

How it works

  1. The outer loop visits every number from low to high. max(low, 2) skips 0 and 1, which are not prime.
  2. The inner loop tries divisors up to the square root of n.
  3. When a divisor is found, break leaves the inner loop at once, and the else block is skipped.
  4. If the inner loop runs to the end without break, the else block runs and prints n. end=" " keeps the primes on one line, and the final print() ends that line.

Watch it run line by line

Step through n = 15: the inner loop tries d = 2, then d = 3 hits break and the highlighted line jumps back to the outer for. For n = 11 the inner loop ends normally and the else line runs.

“Visualize step by step” opens the program in the Python Compiler and records every step in your browser. Use the arrow keys to move forwards and backwards; changed variables are highlighted.

Another way: Sieve of Eratosthenes

For large ranges, cross out multiples instead of testing each number. The sieve marks every multiple of each prime as not prime; what remains are the primes. It finds all primes below a million in about a second, where testing each number separately takes much longer.

Python
def primes_up_to(limit):
    is_prime = [True] * (limit + 1)
    is_prime[0] = is_prime[1] = False
    for p in range(2, int(limit ** 0.5) + 1):
        if is_prime[p]:
            for multiple in range(p * p, limit + 1, p):
                is_prime[multiple] = False
    return [n for n, prime in enumerate(is_prime) if prime]

print([p for p in primes_up_to(50) if p >= 10])
Output
[11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

Common mistakes

  • Putting else under the if instead of under the for: then it prints n once for every divisor that does not divide it.
  • Using range(low, high), which leaves out high itself.
  • Starting the sieve's inner loop at 2 * p instead of p * p: still correct, but it repeats work already done by smaller primes.

Try it yourself

  • Print how many primes there are between 1 and 1,000 (there are 168).
  • Find twin primes: pairs such as (11, 13) that differ by 2.

Open the program in the Python Compiler, change it and press Run: there is nothing to install, and your code never leaves your browser.

Browse all 25 Python examples, or write your own program in the online Python Compiler.