Skip to content
Devtoolry

Numbers · beginner

Python Program to Check Prime Number

Check whether a number is prime in Python by testing divisors up to its square root, with a step-by-step explanation and the bugs that make 1 or 9 look prime.

A prime number is a whole number greater than 1 whose only divisors are 1 and itself: 2, 3, 5, 7, 11 and so on. To check a number n, look for a divisor between 2 and n - 1. If you find one, n is not prime.

You only need to test divisors up to the square root of n: if n = a × b and both a and b were bigger than √n, their product would be bigger than n. For 97 that means checking 2 to 9 instead of 2 to 96.

The program

Python
import math

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

for n in [1, 2, 9, 29, 97]:
    print(n, "is prime" if is_prime(n) else "is not prime")
Output
1 is not prime
2 is prime
9 is not prime
29 is prime
97 is prime

How it works

  1. Numbers below 2 (0, 1 and negatives) are not prime by definition, so the function returns False straight away.
  2. math.isqrt(n) is the integer square root: math.isqrt(97) is 9. Adding 1 makes range() include it, because range stops before its end value.
  3. If any d divides n with no remainder (n % d == 0), n has a divisor other than 1 and itself, so the function returns False immediately.
  4. If the loop finishes without finding a divisor, the number is prime. For 2 the range is empty, so 2 is correctly reported as prime.

Watch it run line by line

Visualize the program and follow the call is_prime(9): d takes the value 2, then 3, and 9 % 3 == 0 returns False. For 97 the loop runs all the way to d = 9 before returning True.

“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: With all() and a generator expression

all() returns True when every check passes, and it stops at the first False, just like the early return above. It is shorter but harder to step through.

Python
import math

def is_prime(n):
    return n > 1 and all(n % d for d in range(2, math.isqrt(n) + 1))

print([n for n in range(30) if is_prime(n)])
Output
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Common mistakes

  • Forgetting the n < 2 check, which makes 1 (and 0) look prime because the loop never finds a divisor.
  • Looping to int(n ** 0.5) without + 1, which skips the square root itself, so 9, 25 and 49 are reported as prime.
  • Printing "prime" inside the loop after the first divisor that fails, before all divisors were checked.
  • Looping all the way to n: correct, but very slow for large numbers such as 1,000,000,007.

Try it yourself

  • Count the primes below 10,000.
  • Return the smallest divisor instead of False, and print it: "91 is not prime (7 × 13)".
  • Skip even divisors after 2 to make the loop about twice as fast.

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.