Skip to main content

How to check if a number is prime in Python

How to check if a number is prime in Python.

Here's a detailed step-by-step tutorial on how to check if a number is prime in Python.

Step 1: Understand Prime Numbers

Before we start writing code, let's understand what prime numbers are. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, a prime number is only divisible by 1 and itself.

Step 2: Plan the Algorithm

To check if a number is prime, we can use a simple algorithm that involves checking if the number is divisible by any number less than itself (excluding 1). If the number is divisible by any of these numbers, it is not prime. Otherwise, it is prime.

Step 3: Write the Code

Let's start writing the code to check if a number is prime. We'll define a function called is_prime() that takes a number as input and returns True if it is prime and False otherwise.

def is_prime(num):
if num <= 1:
return False
for i in range(2, num):
if num % i == 0:
return False
return True

In the code above, we first check if the number num is less than or equal to 1. If it is, we return False because prime numbers are greater than 1.

Next, we use a for loop to iterate over a range from 2 to num-1 (excluding num). We check if num is divisible by any number in this range. If it is, we return False because it means the number is not prime.

If the loop completes without finding any divisors, we return True because it means the number is prime.

Step 4: Test the Code

Now that we have written the code, let's test it with some examples:

print(is_prime(7))    # Output: True
print(is_prime(12)) # Output: False
print(is_prime(29)) # Output: True
print(is_prime(1)) # Output: False

In the first example, the number 7 is prime, so the function returns True. In the second example, the number 12 is not prime because it is divisible by 2, 3, 4, 6, and 12, so the function returns False. In the third example, the number 29 is prime, so the function returns True. In the last example, the number 1 is not prime because it is less than or equal to 1, so the function returns False.

Step 5: Optimize the Code (Optional)

The above code works correctly, but it can be optimized further. We can improve the efficiency of the algorithm by checking divisibility up to the square root of the number, rather than num-1. This is because if a number num is divisible by a factor greater than its square root, it must also be divisible by a factor smaller than its square root.

Here's an optimized version of the code:

import math

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

In this version, we use the math.isqrt() function to calculate the square root of num and convert it to an integer. We then check divisibility up to this square root value.

Conclusion

You now have a detailed step-by-step tutorial on how to check if a number is prime in Python. By following these steps and using the provided code examples, you can easily determine if a given number is prime or not.