# Python Program to Check Whether a Number is Prime or Not

In this python tutorial, you will learn how to Check Whether a Number is Prime or Not using the if and else statement and for loop along with the different operators of the python programming language.

## How to Check Whether a Number is Prime or Not?

Let’s take a look at the source code , here the values are given as input by the user in the code, the if, else statement and for loop along with the assignment operators carry out the function.

```# Program to check if a number is prime or not using loop

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

if num > 1:
for i in range(2,num):
if (num % i) == 0:
print("\n", num,"is not a prime number")
break
else:
print("\n", num,"is a prime number")

else:
print("\n", num,"is not a prime number")```

INPUT:

`59`

OUTPUT:

```Enter a number:
59 is a prime number```
1. Here we give the user the option to enter the values and the input values are scanned using the `input`  function and are stored in the variable `num`  with the statements/strings `("Enter a number: ")`,  we use the `int`  function and declare the  input value as an integer value.
2. In the STDIN section of the code editor the input values are entered.
3. Next we declare the `if`  statement followed by a `:` with condition `num > 1` where if the condition is satisfied, it moves to the next statement, that is the `for` loop, if the condition is not satisfied it will move to the last step which is the `else`  statement.
4. In the last `else` statement, the `print` function will display the statement `("\n", num,"is not a prime number")` where the variable `num` holds the input integer value.
5. Following the first `if` statement, we open a `for` loop where the `range` for the variable `i` is `(2,num)` followed by a `:`.
6.  In the `for` loop, we declare an `if` statement with condition `(num % i) == 0` followed by a `:`, if the condition is satisfied, the `print` function will display the statement `("\n", num,"is not a prime number")` and `break` statement terminates the current loop.
7. In the above line of code, the value of `num` will be within the given range `(num % i) == 0`, and the loop will iterate until the condition is satisfied.
8. If not it moves the next step which is the `else` statement, where the `print` function will display the statement `("\n", num,"is a prime number")`.

NOTE:

• For loop runs a block of code until the loop has iterated over every item in a condition, that is it helps to reduce any repetition in the code because it executes the same operation multiple times.
• The Range() function is used to generate a sequence of numbers, where the range() function is commonly used in for looping functions.
• The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
• The Break statement in Python terminates the current loop and resumes execution at the next statement.
• The print statement/string to be displayed in enclosed in double quotes.
• The input() function allows a user to insert a value into a program, it returns a integer value.
• The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
• The statement for the input function are enclosed in single quotes and parenthesis.
• The \n in the code indicates a new line or the end of a statement line or a string.