Summary

Learn how to write a Python program to check if a number is prime. This guide includes beginner-friendly code and step-by-step explanations for easy understanding.

Article Body

How to Check if a Number is Prime in Python with Simple Code and Explanation
How to Check if a Number is Prime in Python with Simple Code and Explanation

prime number is a number that is greater than 1 and has no divisors other than 1 and itself. In this article, you will learn how to write a Python program to check whether a number is prime or not, and we'll explain everything in a simple way.


πŸ”’ What is a Prime Number?

A prime number has only two factors:

  • 1

  • The number itself

πŸ“Œ Examples of prime numbers:

  • 2, 3, 5, 7, 11, 13, 17, 19...

πŸ“Œ Examples of non-prime (composite) numbers:

  • 4 (divisible by 2), 6 (divisible by 2 and 3), 9 (divisible by 3)


πŸ’» Python Program to Check Prime Number

Here’s a simple and clean Python program:

# Program to check if a number is prime

# Step 1: Take input from the user
num = int(input("Enter a number: "))

# Step 2: Check if the number is less than or equal to 1
if num <= 1:
    print(num, "is not a prime number")
else:
    # Step 3: Use a loop to check for factors
    is_prime = True
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            is_prime = False
            break

    # Step 4: Print the result
    if is_prime:
        print(num, "is a prime number")
    else:
        print(num, "is not a prime number")

🧾 Step-by-Step Explanation of the Code

βœ… Step 1: Take User Input

num = int(input("Enter a number: "))
  • We use input() to ask the user to enter a number.

  • int() converts the input from a string to an integer.


βœ… Step 2: Check for Edge Cases

if num <= 1:
    print(num, "is not a prime number")
  • A number less than or equal to 1 is not prime (e.g., 0, -5, 1).

  • We handle this before doing any further checks.


βœ… Step 3: Check for Factors Using Loop

is_prime = True
for i in range(2, int(num ** 0.5) + 1):
    if num % i == 0:
        is_prime = False
        break

How does this work?

  • We assume the number is prime with is_prime = True.

  • We check all numbers from 2 to square root of num.

    • Why square root? Because if a number has a factor greater than its square root, it must also have a smaller factor. This reduces the number of checks.

  • If we find any number that divides num evenly (num % i == 0), we know it's not a prime.


βœ… Step 4: Show the Result

if is_prime:
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")
  • Based on our loop, we print the final result to the user.


  • Based on our loop, we print the final result to the user.


βœ… Example Output

Enter a number: 7
7 is a prime number

Enter a number: 10
10 is not a prime number

πŸ€” What If We Don’t Use Square Root Optimization?

If we check up to num - 1, the program will still work, but it will be slower, especially for large numbers.

This is the less efficient way:

for i in range(2, num):
    if num % i == 0:
        is_prime = False
        break

πŸ“Œ Use the square root method for better performance.


βœ… Bonus: Create a Function to Check Prime

You can also make a reusable function to check if a number is prime:

def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

# Test the function
num = int(input("Enter a number: "))
if is_prime(num):
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")

This way, you can reuse the is_prime() function in other programs.


πŸ“š Summary

  • A prime number has only two divisors: 1 and itself.

  • We use a for loop to check if the number has any other divisors.

  • Using square root optimization improves performance.

  • We can also turn the code into a function for reusability.


πŸ™‹β€β™‚οΈ Frequently Asked Questions

πŸ”Έ Is 1 a prime number?

No, by definition, 1 is not a prime number.

πŸ”Έ Why check up to the square root?

Because if a number has a factor greater than its square root, the matching pair factor must be smaller.

πŸ”Έ What is the smallest prime number?

The smallest prime number is 2. It is also the only even prime number.