# Python Examples For Beginners| Check Prime Number

Thank you very much to all the lovely people around the world reading my last article Python Examples for Beginners. If you haven’t got a chance to see that just have a look once (a kind request).

So, we learn how to add two numbers in Python, now it’s time to go one step forward therefore we will see today how to check whether a number is prime or not? Sound’s great….

# Python Examples For Beginners| Check Prime Number

In this article what we will do, we first take an user input and then we apply prime number methodologies in Python. For this just have a look at the below code.

By the I believe if you are reading this much that means you already have some sort of programming experience and basic knowledge of Python. If not, just take a look of the Python Documentations.

Well, now we are all set to jump into our code base…

To check a number whether it is prime or not we first take some user input and then only we can calculate or rather find out whether that number is prime or not.

Now see below the entire code first and then we will explain each and every part of this program.

# take user input a = input("Enter the number: ")

a = int(a) //by-default user inputs are str so to calculate further we need to cast it to int x = 0

if (a > 3):

for i in range (2, a):

if(a % i) == 0:

x = 1;

break

else:

x = 0 if(x == 1):

print(a, "is not a prime number")

else:

print(a, "is a prime number")

# Explanations:

Now explaining each part of this program.

First thing first,

# Take an user input

`# take user input `

a = input("Enter the number: ")

a = int(a) //by-default user inputs are str so to calculate further we need to cast it to int

**Initialization and condition**

Now, we have user input in our hand. So what we will do next? Simple we have to check whether that number is divisible by other number of not except 1 and that number itself, right? That is the prime number definition. I guess we all do that in our childhood days.

Let’s see this code…

Why we have taken a variable called x = 0. Why? the reason is very simple it is flag kind of thing. You will understand in couple of lines later.

We have checked a > 3 and then enters into the program, why? It simply because we know that 1, 2 and 3 are prime numbers so there is no meaning to checking 1, 2 and 3. To smartly calculate we can start our program from 4 that is a > 3.

# Start the loop

Now, as we see in the definition, we have to check whether that number is divisible by any number. So to find out that any we have to check from the beginning. So the loop shroud start from the beginning, like

`for i in range(1, a):`

But in our case we have written

`for i in range(2, a):`

What is actually does, the loop is creating the divisors. As we know the the number should not be divisible by 1 and that number itself that means we have to ignore 1 and that user input number. So we have start the loop of divisor from 2 instead of 1 right. You may also ask how we ingore that number itself, so to answer this you have to just check the loop in python. If you see python for loop starts from 0th index so range(2,5) meaning it will execute like 2, 3 and 4. Hope now this is clear.

Moving next, we have write

`if(a % i) == 0: `

x = 1;

break

This is the core part of this program, after starting the loop from 2 to user input, then we check whether the user input number is divisible by each loop index that is denoted here “i”. Now again you may confuse, I am talking division but not using the division operator “/”. Instead we used “%” what that means? It means the “%” modulo operator actually returning the reminder of a division. So in our case if the user input number divided by the loop index and if the reminder is 0 that means the user input number is not a prime number.

Now you may think, ok I can check whether the reminder is 0 or not and then I can write that number is prime or not why I use x = 1 and break? If that questions arise in your mind that is fine it quite obvious as a beginner this questions are very much appreciable. Ok, let see why we use that x and break.

Assume, we write

`if(a % i) == 0: `

print(a, " is not a prime number")

else:

print(a, " is a prime number")

So, if we write like this, what will happen? Let the user input is 4 then the loop executes and it will start dividing the number with 2 and its getting 0 reminder and display “4 is not a prime number”. Fine but the loop will not end it will now move to 3, now this time the reminder will be 1 so it will print “4 is a prime number”. So you can see as long as the loop iterates it will print this and that so that will not solve the purpose. Hence we need to think wisely, see if the number divided by any number other that 1 that means that number is not a prime and if that is happening once that’s enough to judge that the user input number is not a prime number. So what we have done here we just use a variable which just work as a flag and break the loop if it is able to divide the number. Got it. You can see we have first initialize the x = 0 and it will only change to 1 when the number is divisible. So after executing the entire loop we can say if x = 1, meaning the number is not prime and if it remains 0 that means the number is prime. Cool!!!

# Output

`Please enter the number: 7 `

7 is a prime number

Hope we can make you understand how we can check a number is prime or not in python. We will come up with more interesting examples for beginners.

Please keep following us on other social media platforms.

*Originally published at **https://www.codinghub.net**.*