Last modified: Nov 16, 2026
Python Prime Number Check Program
A prime number is a number greater than 1 that has only two divisors: 1 and itself. Examples include 2, 3, 5, 7, and 11.
Checking for primes is a classic beginner exercise. It teaches loops, conditions, and basic math logic. This guide walks you through several ways to build a prime check program in Python.
What Is a Prime Number?
A prime number cannot be divided evenly by any number except 1 and itself. The number 2 is the only even prime.
Numbers like 4, 6, 8, and 9 are not prime. They are called composite numbers because they have more than two divisors.
The number 1 is not prime. It has only one divisor, so it fails the definition.
The Basic Prime Check
The simplest approach is to loop from 2 up to the number minus one. If any number divides evenly, it is not prime.
def is_prime(n):
# Numbers less than 2 are never prime
if n < 2:
return False
# Check every number from 2 to n-1
for i in range(2, n):
if n % i == 0:
return False
return True
print(is_prime(11))
print(is_prime(15))
True
False
This works well for small numbers. But it is slow for large numbers because it checks every value below n.
If you plan to pass several values into a helper like this, it helps to understand how parameters flow, which you can review in this guide on Python function arguments explained.
Optimized Check Using Square Root
You only need to check divisors up to the square root of n. If no divisor exists below that point, none exists above it either.
This cuts the loop length dramatically. For example, checking 10,000 needs only 100 iterations instead of 9,999.
import math
def is_prime(n):
if n < 2:
return False
# Only check up to the square root
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
print(is_prime(97))
print(is_prime(100))
True
False
This version is much faster. It is the version most developers use in real code.
Even Faster: Skip Even Numbers
After handling 2 separately, you can skip all even divisors. This halves the loop work again.
import math
def is_prime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
# Step by 2 to skip even numbers
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
print(is_prime(101))
print(is_prime(99))
True
False
This trick is simple but effective. It is a common interview answer.
Print All Primes in a Range
You can reuse the function to list every prime up to a limit. Store results in a list when order matters, and compare containers in this article on Python set vs list key differences.
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
primes = [x for x in range(2, 30) if is_prime(x)]
print(primes)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
List comprehensions keep the code short and readable. They are a natural fit here.
Using a While Loop
You can also build the check with a while loop. It gives you manual control over the counter.
def is_prime(n):
if n < 2:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
print(is_prime(13))
print(is_prime(21))
True
False
The condition i * i <= n avoids importing math. It is a neat alternative to the square root version.
The Sieve of Eratosthenes
When you need many primes at once, the Sieve of Eratosthenes is the best choice. It marks multiples of each prime as composite.
def sieve(limit):
# Start by assuming every number is prime
is_prime = [True] * (limit + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(limit ** 0.5) + 1):
if is_prime[i]:
# Mark all multiples of i as not prime
for j in range(i * i, limit + 1, i):
is_prime[j] = False
return [i for i, prime in enumerate(is_prime) if prime]
print(sieve(20))
[2, 3, 5, 7, 11, 13, 17, 19]
The sieve is far faster than calling a prime check in a loop. Use it when the upper limit is known in advance.
Common Mistakes to Avoid
Beginners often forget to handle numbers below 2. This causes 0 and 1 to be reported as prime, which is wrong.
Another mistake is looping up to n instead of sqrt(n). This still works but wastes time.
Also, remember that 2 is prime. Skipping the even check for 2 will break your function.
Finally, keep your code clean. Small helpers like rounding values for display are easy to get wrong, so check this guide on Python round to 2 decimal places when formatting output.
Testing Your Prime Function
Always test with edge cases. Try 0, 1, 2, 3, and a large prime like 7919.
tests = [0, 1, 2, 3, 4, 7919, 7920]
for t in tests:
print(t, is_prime(t))
0 False
1 False
2 True
3 True
4 False
7919 True
7920 False
Passing all these cases gives you confidence the function is correct.
Performance Comparison
The basic loop and the square root version give the same answers. The difference is speed.
For a number near one million, the basic loop may run hundreds of thousands of checks. The square root version runs about a thousand.
Choose the right tool for the job. Use a simple check for one number. Use the sieve for many numbers.
Conclusion
Writing a prime number check in Python is a great way to practice loops and conditions. Start with the basic version to learn the logic.
Then move to the square root version for speed. Add the even-number skip for extra efficiency. Use the sieve when you need lots of primes.
Test your function with edge cases and keep your code clean. With these techniques, you can check primes reliably in any Python project.