News Blog Fact Check Press Release Jobs Event Product FAQ Local Business Lists Live Music Recipe

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

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.

Published on

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

Google Advertisement

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.

  • Google Advertisement

    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.


Google Advertisement

โœ… 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.

Want to engage with this content?

Like, comment, or share this article on our main website for the full experience!

Go to Main Website for Full Features

Rahul Kumar

Software Engineer & Tech Editor

Rahul is a software engineer and editor at Galaxy Founder, passionate about technology, startups, and digital innovation. With a keen eye for emerging trends and a love for clean, efficient code, Rahul shares insights and resources to help others navigate the evolving tech landscape.

More by this author โ†’

Published by ยท Editorial Policy

Galaxy Founder | Latest News, Job Updates & In-Depth Product Reviews โ€” Galaxy Founder brings you the latest news, real-time job postings, and honest product reviews to keep you informed, updated, and ahead. Discover trusted content across trending topics only on Galaxy Founder.

๐Ÿ‘‰ Read Full Article on Website