Table of contents
1.
Introduction
2.
What is Prime number in Python?
2.1.
Understanding the Problem Statement
2.1.1.
Examples for Prime number in Python
3.
Python Program to Check Prime Number Using Math Module
3.1.
Python
4.
Python Program to Check Prime Number Using sympy.isprime() Method
4.1.
Python
5.
Using a Flag Variable
5.1.
Python
6.
Checking Prime Numbers Using a While Loop
6.1.
Python
7.
Prime Number Generator Using a Generator Function
7.1.
Python
8.
Prime number in Python-What are Prime Numbers?
9.
Prime number in Python-Solution
9.1.
Implementation
9.2.
Python
10.
Check Prime Numbers Using Recursion in Python
10.1.
Python
11.
Frequently Asked Questions
11.1.
What are Prime Numbers?
11.2.
How to find Prime Numbers in Python?
11.3.
What does % mean in Python?
11.4.
How to print prime numbers from 1 to 100 in Python?
11.5.
How to find first n prime numbers in Python?
11.6.
How to get prime factors in Python?
12.
Conclusion
Last Updated: Aug 21, 2025
Easy

Python Program to Check Prime Number

Author Parth Jain
1 upvote

Introduction

Determining whether a number is prime is a fundamental concept in number theory and programming. A prime number is defined as a natural number greater than 1 with no divisors other than 1 and itself. This concept is often used in coding exercises to strengthen understanding of loops, conditionals, and efficiency. In this article, we’ll learn how to check a prime number in Python with a simple program.

Python Program to Check Prime Number

What is Prime number in Python?

In Python, a prime number is a positive integer greater than 1 that has exactly two distinct factors: 1 and itself. This means the number is only divisible by 1 and itself with no remainder. Common examples of prime numbers include 2, 3, 5, 7, 11, and 13.

Python makes it easy to check for prime numbers using different approaches:

  • For loops to test divisibility
     
  • While loops for optimization
     
  • Built-in libraries like sympy.isprime() for efficient prime checking

Understanding the Problem Statement

Given a positive integer N, Write a Prime number program in Python to check if the given number is prime or not.

Examples for Prime number in Python

Input:  n = 1
Output: false

Input:  n = 7
Output: true

Input:  n = 11
Output: true

Input:  n = 4
Output: false


Check out this problem - First Missing Positive 

Python Program to Check Prime Number Using Math Module

The math module in Python provides various mathematical functions, including sqrt(), which can be leveraged to check if a number is prime. By checking divisibility up to the square root of the number, we can significantly reduce the number of iterations needed.

  • Python

Python

import math

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

# Example usage:
number = 29
if is_prime(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
You can also try this code with Online Python Compiler
Run Code

 

Output:

29 is a prime number.

Python Program to Check Prime Number Using sympy.isprime() Method

The sympy library in Python provides a straightforward way to check for prime numbers using the isprime() method. This method is efficient and accurate, making it a great choice for those who prefer using libraries.

  • Python

Python

from sympy import isprime

number = 29

if isprime(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
You can also try this code with Online Python Compiler
Run Code

 

Output:

29 is a prime number.

Using a Flag Variable

A flag variable can be used to keep track of whether a number has been found to be divisible by any other number. This approach is simple and helps in understanding basic control flow in Python.

  • Python

Python

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

# Example usage:
number = 29
if is_prime(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
You can also try this code with Online Python Compiler
Run Code

 

Output:

29 is a prime number.

Checking Prime Numbers Using a While Loop

A while loop can be used as an alternative to a for loop to check if a number is prime. This method is helpful for those who prefer or need to use a while loop for specific scenarios.

  • Python

Python

def is_prime(num):
if num <= 1:
return False
i = 2
while i <= num // 2:
if num % i == 0:
return False
i += 1
return True

# Example usage:
number = 29
if is_prime(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
You can also try this code with Online Python Compiler
Run Code

 

Output:

29 is a prime number.

Prime Number Generator Using a Generator Function

A generator function can be used to create an iterator that yields prime numbers one at a time. This approach is useful when you need to generate a series of prime numbers rather than checking a single number.

  • Python

Python

def prime_generator(limit):
for num in range(2, limit + 1):
if is_prime(num):
yield num

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

# Example usage:
for prime in prime_generator(50):
print(prime, end=" ")
You can also try this code with Online Python Compiler
Run Code

 

Output:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 

Prime number in Python-What are Prime Numbers?

A positive integer greater than 1 and does not have any other factors except 1, and the number itself is known as a  prime number. For Example, the numbers  5, 7, and 11 are prime numbers as they do not have any other factors except 1 and the numbers themselves.

Prime number in Python-Solution

To solve this prime number in Python problem, we need to iterate through all the numbers starting from 2 to (N/2) using a loop, and at every number, we check if it divides N. In case we find a number that divides N, we return false. If we do not find any number between 2 and N/2 which divides N, then it means N is prime, and we will return True.

Implementation

  • Python

Python

#Program to find Prime number in Python 
num = 7
# Check if the number is greater than 1
if num > 1:
 #This is an interactive prime number program in python
   # Iterating from 2 to n / 2
   for i in range(2, int(num/2)+1):

       # If the number is divisible by any number in between
       # 2 and n / 2, it is not prime
       if (num % i) == 0:
           print(num, "is not a Prime Number")
           break
   else:
       print(num, "is a Prime Number")

else:
   print(num, "is not a Prime Number")
You can also try this code with Online Python Compiler
Run Code

Output for Prime number in Python program:

11 is a prime number

Check Prime Numbers Using Recursion in Python

To check if a number is prime using recursion in Python, the approach is to divide the number by smaller numbers (starting from 2) and recursively check if the division leaves a remainder.

  • Python

Python

def is_prime(n, i=2):
# Base case: if number is less than or equal to 1, it's not prime
if n <= 1:
return False
# If i reaches n, number is prime
if i == n:
return True
# If n is divisible by i, it's not prime
if n % i == 0:
return False
# Recursively check next divisor
return is_prime(n, i + 1)

# Input
num = int(input("Enter a number: "))

if is_prime(num):
print(f"{num} is a Prime Number")
else:
print(f"{num} is Not a Prime Number")
You can also try this code with Online Python Compiler
Run Code

 

Output

Enter a number: 7
7 is a Prime Number

Enter a number: 10
10 is Not a Prime Number

Frequently Asked Questions

What are Prime Numbers?

A positive integer greater than 1 does not have any other factors except 1, and the number itself is known as a  prime number.

How to find Prime Numbers in Python?

To detect a Prime Number in Python, we need to iterate through all the numbers starting from 2 to (N/2) using a loop, and at every number, we check if it divides N. In case we find a number that divides N, we return false. If we do not find any number between 2 and N/2 which divides N, it means that N is a prime number, and will return True.

What does % mean in Python?

The % symbol is known as the Modulus operator and is one of the arithmetic operators available in Python. The modulus operator provides the remainder between two numbers.
If x = 5 and y = 2
Then x % y will return 1.

How to print prime numbers from 1 to 100 in Python?

To print prime numbers from 1 to 100, you can use a loop to iterate through this range, checking each number for primality using a function like is_prime(), and then print the prime numbers.

How to find first n prime numbers in Python?

To find the first n prime numbers, you can use a loop to generate numbers and a function to check primality, collecting the primes until you reach n, then print or store them as needed.

How to get prime factors in Python?

To get prime factors in Python, iteratively divide the number by the smallest prime until it's 1, incrementing the divisor when it no longer divides cleanly.

Conclusion

In conclusion, we explored different ways to check prime numbers in Python, from basic loops to libraries like sympy. Each approach helps build problem-solving skills and offers insights into algorithm design and optimization.

Recommendation Reading: 

Live masterclass