Python Program to Check Prime Number for beginners | Python Example

Python Program to Check Prime Number 

In this post, we will learn, how to check the input is a prime number or not, using a simple python program. And we will also check some test cases to check our python program will work properly or not. And if you don't know any things about python then don't worry, we will make a simple program that you easily understand very well.

Our first task is to Know, What is a prime number?

A prime number is a number that's is divisible by itself and 1 is called a prime number.

Example: 2, 3, 5, 7, 11 etc.

Is anyone make a python program? How we make a python program?

Python program also thinks and work like the human if we think to add two number such as 2+3=5, then python program also thinks like this, just we will write the line of code in the language of python.

So, if we check the input is a prime number or not then we need to take user input, therefore first we will take the input from the user by python language. python language has its own syntax to take input:


  input("Enter the positive number")

And we store this above line into the variable named 'num'.


    num = input("Enter the positive number")

Now our second step to do some experiment with input according to our needs and our needs to check the input is a prime number or not. We all know that the prime number is always positive and 1 is not a number. So, we check the number the input is greater than 1 and if the number is not greater than 1 then we will print not a prime number.


  if num > 1:

    go to second step.....


    print("Not a Prime") 

Now we divide this number with itself and above this number. For example, the number is 5, then we divide this number with 5,4,3,2.

Now in python language first we will run the for loop from 2 to input. And divide the input with the for loop.


    for i in range(2,num):  

    num % i

Now we will check the division is equal to zero or not.  if the division is equal to zero then the number is not prime number otherwise it is a prime number.


    for i in range(2,num):   
        print("Number is not a prime")
        print("Number a prime number") 

 Whole python Program Code:
# Program to check if a number is prime or not

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

# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(num,"is a prime number")

# if input number is less than
# or equal to 1, it is not prime
print(num,"is not a prime number")

If you have any doubts related to the above code then please comment below.

Next Post Previous Post
  • Ankur Ranpariya
    Ankur Ranpariya September 6, 2020 at 1:23 PM

    good content

Add Comment
comment url