# Python program to check prime number

In this post, we will see how to check prime number in python.

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

## Problem

Given the number check if number is prime or not.

## Solution

• Iterate over 2 to num by using range(2,num)
• If num is divisible by any number between 2 to num, then it is not a prime number
• Otherwise it is a prime number

### Program to check prime number

``` def isPrime(num): for i in range(2,num): if(num%i==0): return False return True print("is 13 prime number: ",isPrime(13)) print("is 28 prime number: ",isPrime(28)) ```

Output:

is 13 prime number: True
is 28 prime number: False

## More optimized solution

You can iterate over 2 to Math.sqrt(num) rather than upto num.

``` import math def isPrime(num): for i in range(2,int(math.sqrt(num))): if(num%i==0): return False return True print("is 13 prime number: ",isPrime(13)) print("is 28 prime number: ",isPrime(28)) ```

Output:

is 13 prime number: True
is 28 prime number: False

Please note that you need to import math to call math.sqrt function in python.
That’s all about How to check Prime number in python