A prime number is a natural number greater than 1 that has exactly two positive divisors: 1 and itself. If you are learning Python, learning how to find out if a number is prime in Python is a great way to practice conditionals, loops, functions, and basic number theory. In Python, you can check whether a number is prime using several approaches, from a simple beginner-friendly method to more optimized versions that run faster for larger numbers.
Introduction to Prime Numbers
A prime number is a whole number greater than 1 that cannot be divided evenly by any number except 1 and itself.
Examples of prime numbers include:
- 2
- 3
- 5
- 7
- 11
- 13
- 17
- 19
As an example, 7 is prime because its only divisors are 1 and 7. That said, 9 is not prime because it can be divided evenly by 3.
The number 1 is not considered prime because it has only one positive divisor, not two. The number 0 and all negative numbers are also not prime.
In Python, checking whether a number is prime usually means writing a function that returns True if the number is prime and False if it is not Surprisingly effective..
What Does “Prime” Mean in Python?
The moment you write a prime number checker in Python, you are usually trying to answer this question:
Does the number have any divisor other than 1 and itself?
If the answer is no, the number is prime. If the answer is yes, the number is composite.
For example:
def is_prime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
This function checks every number from 2 up to n - 1. If any number divides evenly into n, then n is not prime Turns out it matters..
Example usage:
print(is_prime(7))
print(is_prime(10))
print(is_prime(1))
Output:
True
False
False
This version is easy to understand, but it is not the most efficient way to check for primes Not complicated — just consistent..
A Better Basic Approach: Check Up to the Square Root
You do not need to check every number from 2 to n - 1. If a number n has a divisor larger than its square root, then it must also have a matching divisor smaller than its square root.
To give you an idea, consider 100:
100 = 10 × 10
100 = 4 × 25
100 = 2 × 50
If there is a large factor like 25, there is also a smaller factor like 4. This means checking beyond the square root is unnecessary The details matter here. Worth knowing..
A more efficient version looks like this:
def is_prime(n):
if n <= 1:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
This version still checks every number from 2 up to the square root of n, but it avoids unnecessary checks.
Example:
print(is_prime(29))
print(is_prime(49))
print(is_prime(97))
Output:
True
False
False
Why is 49 not prime? Because:
49 = 7 × 7
Why is 97 not prime? Because 97 is actually prime. The output False above is incorrect, so let’s correct the example:
print(is_prime(97))
Output:
True
The important idea is that i * i <= n is a clean way to check whether you have reached the square root of the number Which is the point..
Handling Common Edge Cases
Before testing divisibility, you should handle common cases:
def is_prime(n):
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
i = 3
while i * i <= n:
if n % i == 0:
return False
i += 2
return True
This version improves performance by skipping even numbers after checking whether the number is 2.
Here is what this function does:
- Returns
Falseif the number is less than or equal to 1. - Returns
Trueif the