# Learn Python - Using If and While to find Prime Numbers

**This tutorial is a basic introduction to python.** I will break down basic concepts like

- While Loops
- If Statements
- Taking User Input
- Printing
- How to Break Down and Manage Your Code

One of the most important concepts in programming is the concept of efficiency. If we need a task completed many times, we could write it out each and every time, or we could use a loop!

A loop is a chunk of code that we reuse over and over. Today we will use a **while loop** to calculate prime numbers!

A **prime number** is a number that can not be evenly divided by any two real numbers. For example the number **17** is a prime number. But how can we find these numbers? Using Python!

## Before We Start Coding

Just like any program, we do not start with writing cold hard code. First we have to figure out what our program is trying to achieve, and how we can achieve that goal.

**1.**Our program is going to get **input** from the user.

**2.**Our program is going to **check** if that number is a prime number.

**3.**Our program is going to **tell** the user if their number is a prime number.

Now we can start to make our program by converting these steps into code.

## Coding

In order to code in python all you need a text editor. You can use vim, gedit, or any other text editing program and save as a .py file. Any time you want to compile and run your code you will have to open a terminal, navigate to where your file is, and use

python your_file_name.py

We will start with our **Part 1** from above. *Our program is going to get input from the user.*

In python you simply write

input('Please enter a number')

That line will produce the following result.

Now we need to do something with that number. First we store it a variable.

number = input('Please enter a number')

The users number is now stored in a variable and can be used throughout our program.

We are on to **Part 2!** *Our program is going to check if that number is a prime number.*

How do we check if a number is prime? We would have to divide it by every number possible and see if it ever goes in even! That would take forever! Not with a loop.

A loop will run a chunk of code over and over, either for a certain number of times or until a parameter is met. So if we enter the number 17, we would have to do 17/2 then 17/3 then 17/4 then 17/5 then... and so on until 17/16.

**Notice** how we start dividing by 2. This is because 17/0 is invalid and any number can be divided by 1.

So we will make a variable with the starting value of 2.

i = 2

This line is very simple, but the concept is slightly more complex. Our i variable acts as an **i**ndex that will CHANGE every time the program runs through the loop.

So we need a loop that divides our input number by our index i. i will increase **while** it is one less than our number.

So we will use a **while** loop!

while i<number:

number/i

i = i + 1

This loop will run while i is less than number, adding 1 to i each time. However, nothing is getting done! We need to check **if** i goes into number evenly!

Well lets use an **if** statement!

We will also need the modulus operator. % is the symbol for modulus, and gives is the remainder of any division. 5%2 = 1 because 2 goes into 5 2 times, with a remainder of 1.

So **if** there is NOT a remainder then i does go into our number evenly. Lets modify our loop now.

while i<number:

if number%i == 0:

print ("Your number is NOT a prime number!");

i = i + 1

print("Your number is a prime number");

If you noticed we were also able to complete **Part 3**!3 *Our program is going to tell the user if their number is a prime number.*

The second print statement is OUTSIDE of our while loop. This is because if we can loop through all the number s and not a single one goes in even, then we know it is a prime number.

All together our program looks like this

number = number = input('Please enter a number: ')

i = 2

while i<number:

if number%i == 0:

print ("Your number is NOT a prime number!");

i = i + 1

print ("Your number is a prime number!");

There is one problem with this. Your number is a prime number! Will always print! We need a toggle to make sure it does not print if it is not a prime number.

number = number = input('Please enter a number:')

i = 2

toggle = 0while i<number:

if number%i == 0:

toggle = 1

print ("Your number is NOT a prime number!");

i = i + 1

if toggle == 0:

print ("Your number is a prime number!");

You can see I added a variable toggle, that we turn if our number is not a prime number.

We are ready to run our program! Make sure to save your program with the .py file type. I named mine prime.py. In order to compile and run your program we will need to use the terminal. Navigate to where your program is located in the terminal.

Compile and run with the following command

python prime.py

Show off to your friends!

Check back soon for an expanded version of this program where we check for all the prime numbers between two numbers!

As always feel free to leave a comment with any questions or problems you may have.

### You might also like

**Filed Under :** PROGRAMMING

Don't you think that this script will repeat the output of not prime multiple times. I guess it should be out of while loop and should be under if else after while completion with the comparison of toggle value

#!/usr/bin/python

var=input()

a=2

toggle=0

while(a<var):

if(var%a==0):

toggle=1

a=a+1

if(toggle==0):

print("Your number is Prime")

else:

print("Your number is not Prime")