iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use a function that rejects integers below 2, then tests divisors from 2 through the integer square root of the input. The program below returns True for a prime and False otherwise, then prints a message for the entered integer.
Python prime number program
This version uses math.isqrt to calculate an exact integer square-root bound. It works in Python 3.8 and later.
from math import isqrt
def is_prime(n):
if n < 2:
return False
for divisor in range(2, isqrt(n) + 1):
if n % divisor == 0:
return False
return True
number = int(input("Enter an integer: "))
if is_prime(number):
print(f"{number} is prime")
else:
print(f"{number} is not prime")
For example, entering 9 prints 9 is not prime, while entering 11 prints 11 is prime.
How the function decides whether a number is prime
A prime number is an integer greater than 1 whose only positive divisors are 1 and itself. That definition rules out 1, zero, and negative integers before any divisor checks.
#1 Best Overall
Reject values below 2
The first condition, if n < 2, returns False for every integer below 2. This check matters because the divisor loop has no iterations for small values; without the early rejection, such a value could incorrectly reach return True.
Test divisors only through the square root
The expression n % divisor == 0 checks whether division leaves a remainder. If it is zero, the candidate divisor divides n evenly, so n is not prime. Python defines % as the remainder operator in its language reference.
Rank #2
There is no need to test every integer below n. Any factor greater than the square root must pair with a factor smaller than the square root, so a factor pair will already be found by the time the search reaches that boundary. The loop includes the boundary: Python’s range excludes its stop value, so isqrt(n) + 1 makes the integer-square-root value eligible for testing.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
The Python math.isqrt reference defines it as the floor of the exact square root for a nonnegative integer; it was added in Python 3.8. Using an integer bound avoids relying on a floating-point square root, which may be less suitable for very large integers.
Return a result, then print it
is_prime returns a Boolean rather than printing from inside the function. That separation makes the test reusable: another part of a program can use the result in a condition, store it, or display it differently. Returning False as soon as a divisor is found also avoids unnecessary checks.
Examples and common boundary mistakes
2and3are prime. Their loops have no divisor candidates, and both are greater than 1.4is not prime because divisor 2 divides it evenly.9is not prime because the loop includes divisor 3, its square root.1and negative integers are not prime because the function returns before entering the loop.
A frequent off-by-one bug is using range(2, int(n ** 0.5)) without adding one. Since the stop value is excluded, that version skips the square-root divisor for perfect squares such as 9. The isqrt(n) + 1 bound avoids that error.
When to use a different prime-number algorithm
Checking one integer
Trial division is a straightforward choice when the task is to determine whether one integer is prime. Its small, readable function also makes the return behavior easy to understand.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Generating every prime up to a limit
If the task is to list all primes up to a fixed maximum, use the Sieve of Eratosthenes instead of repeatedly calling a single-number checker. A sieve marks composite numbers across the range and collects the candidates that remain. The range-generation approach is presented as faster for that task in Al Sweigart’s *Cracking Codes with Python* chapter on finding and generating prime numbers.
Best Value
Working with extremely large integers
Simple trial division becomes impractical for extremely large values. The same chapter introduces Rabin–Miller for that scale. The classroom function here is an introductory primality check, not a guarantee of cryptographic suitability.
Quick Recap
References
- Python 3.14.8 language reference: expressions
- Python 3.14.8 standard library:
math.isqrt - Al Sweigart, *Cracking Codes with Python*, chapter on prime numbers
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

