Programming Tutorials Hub

Learn Python with simple code, clear explanations, and examples

Python program to check whether a given number is Prime or not

📄 Code

number = input("Enter a number:")
number = int(number)
flag = False

if number == 0 or number == 1:
    flag = True
else:
    for i in range(2, number//2+1):
        if number % i == 0:
            flag = True

if flag:
    print("not prime")
else:
    print("prime")
        
💡 Output
Enter a number:2 
prime

Enter a number:4
not prime
        
🧠 Code Explanation
Prime numbers are positive integers greater than one and are not divisible by any positive integers other than one and the number itself. 

[divisible means remainder is zero]
[not divisible means remainder is non-zero]

Examples of prime numbers and non-prime numbers:

Example #1
3 is a prime number:
3 is only divisible by 1 and 3.

Example #2
5 is a prime number:
5 is only divisible by 1 and 5.

Example #3
4 is a non-prime number:
4 is divisible by 2 othan than 1 and 4.
4 divided by 2 => remainder is zero.


Here i will explain the code with an example.

number = input("Enter a number:")

The input() function is used to get input from the user during runtime. 
For example: if the user input is 3, it will be assigned to the variable 'number' as str(string) object.
number  = "3"

number = int(number)
The number "3" which is stored as string is now get converted into integer using int() function and is assigned to the variable 'number'.
number = 3

flag = False
The variable 'flag' is initialised to boolean value False.
The variable 'flag' is assigned the boolean value 'True' if found any divisor by which the number is divisible.

if number == 0 or number == 1:
    flag = True
else:
    for i in range(2, number//2+1):
        if number % i == 0:
            flag = True

If the positive integer assigned by the user is either 0 or 1, then we need to set the value of the variable 'flag' to True directly without the need to find any divisor by which the number is divisible. Because prime numbers are greater than 1 by definition.

For example, if the user input is 1, then 
number = input("Enter a number:")
number = 1

if number == 0 or number == 1:
	=> if 1 == 0 or 1 == 1:
	=> if False or True:
	=> if True: which is True, therefore the body of the if statement will get executed. 

	flag = True
	The flag is set to True indicating that the user inputted number is a non-prime number.

For example, if the user input is 3, then 
number = input("Enter a number:")
number = 3

if number == 0 or number == 1:
	=> if 3 == 0 or 3 == 1:
	=> if False or False:
	=> if False: which is False, therefore the body of the if statement will not get executed and the Flag value is having the initially assigned value, False.

The if condition is False, therefore, the else statement will get executed. ie. the body of the else statement will get executed.

It means that the else block get executed only if the user input is greater than 1.

The body of the else block is shown below.

for i in range(2, number//2+1):
    if number % i == 0:
        flag = True

So, here the logic is that, if we have the user input greater than 1, we need to check whether the given number is divible by any of the numbers ranging from 2 to the floor value of number/2.

For example, if the given number is 3, we need to check whteher the number is divisible by any of the numbers ranging from 2 to floor of 3/2. ie. 2 to floor(1.5). ie. 2 to 1. We know that we cannot have a sequence ranging from 2 to 1 in ascending order. Therefore the for loop associated with the range function will not get executed atleast once. Therefore the control comes out of the for loop.

for i in range(2, 3//2+1): 
	=> for i in range(2, 1+1):
	=> for i in range(2, 2): Here, the range limit is 2 to end-1, ie. 2 to 2-1, ie. 2 to 1, which can't be True.

Syntax of range function is range(start, stop). 

n//2+1 is provided as the second argument of the range function indicating 'stop' value.

'//' operator is used for floor division. 
Why we have used n//2+1 instead of n//2 is that, range function will return a sequence ranging from start to stop-1. So, for example, if we want to get a sequence ranging from 0 to 1, then we need to provide the range function as 'range(0, 2)' instead of 'range(0,1)'. 


Another example, if the given number is 4, we need to check whteher the number is divisible by any of the numbers ranging from 2 to floor of 4/2. ie. 2 to floor(2). ie. 2 to 2. We know that we will get a sequence ranging from 2 to 2 in ascending order. ie. a single-valued sequence, 2. Therefore the for loop associated with the range function will get executed once. After that program control comes out of the for loop.

Sequence : 2

range(2, 3) means, the sequence ranging from 2 to 3-1. ie. 2 to 2. The sequence will be 2. (single-valued sequence).

#first loop

for i in range(2, 4//2+1): 
	=> for i in range(2, 2+1):
	=> for i in range(2,3): Here, the range limit is 2 to end-1, ie. 2 to 3-1, ie. 2 to 2.

Integer value 2 is assigned to the variable 'i' in this first loop which is True, therefore, the body of the loop will be executed:

if number % i == 0:
        flag = True

if number % i == 0:
	=> if 4 % 2 == 0:
	=> if 0 == 0: which is True, therefore, the body of the if statement will get executed. ie. 'flag = True' will get executed means, variable flag is set to True.  

	flag = True

#second loop [not executed]

for i in range(2, 4//2+1): 
	=> for i in range(2, 2+1):
	=> for i in range(2,3): 
Integer value 3 is not assigned to the variable 'i' in this second loop because the range limit exceeded, therefore, the body of the loop will not be executed and control of the loop comes outside the loop.

if flag:
    print("not prime")
else:
    print("prime")

Now we need to check whether the value of variable 'flag' is True or False. If it is True, the  user input is not a prime number otherwise it is a prime number.

Here in our above example the user input is 4 and the 'flag = True' statement is executed.

if flag:
	=> if True: which is True, therefore, the body of the if statement will get executed. ie. 'print("not prime")' will get executed and the output will be 'not prime'.


Another example, if the given number is 7, we need to check whteher the number is divisible by any of the numbers ranging from 2 to floor of 7/2. ie. 2 to floor(3.5). ie. 2 to 3. We know that we will get a sequence ranging from 2 to 3 in ascending order. The sequence is 2,3. Therefore the for loop associated with the range function will get executed twice. After that program control comes out of the for loop.

Sequence : 2,3

range(2, 4) means, the sequence ranging from 2 to 3-1. ie. 2 to 2. The sequence will be 2,3.

#first loop

for i in range(2, 7//2+1): 
	=> for i in range(2, 3+1):
	=> for i in range(2,4): Here, the range limit is 2 to end-1, ie. 2 to 4-1, ie. 2 to 3.

Integer value 2 is assigned to the variable 'i' in this first loop which is True, therefore, the body of the loop will be executed:

if number % i == 0:
        flag = True

if number % i == 0:
	=> if 7 % 2 == 0:
	=> if 1 == 0: which is False, therefore, the body of the if statement will not get executed.

#second loop

for i in range(2, 7//2+1): 
	=> for i in range(2, 3+1):
	=> for i in range(2,4): Here, the range limit is 2 to end-1, ie. 2 to 4-1, ie. 2 to 3.

Integer value 3 is assigned to the variable 'i' in this second loop which is True, therefore, the body of the loop will be executed:

if number % i == 0:
        flag = True

if number % i == 0:
	=> if 7 % 3 == 0:
	=> if 1 == 0: which is False, therefore, the body of the if statement will not get executed.

#third loop [not executed]

for i in range(2, 7//2+1): 
	=> for i in range(2, 3+1):
	=> for i in range(2,4): 
Integer value 4 is not assigned to the variable 'i' in this third loop because the range limit exceeded, therefore, the body of the loop will not be executed and control of the loop comes outside the loop.

We have seen that the value of the flag value is still having the default value 'False'.

if flag:
    print("not prime")
else:
    print("prime")

Now we need to check whether the value of variable 'flag' is True or False. If it is True, the  user input is not a prime number otherwise it is a prime number.

Here in our above example the user input is 7 and the value of the variable 'flag' is False.

if flag:
	=> if False: which is False, therefore, the body of the if statement will not get executed and the program control goes to the else block and the body of the else block will get executed. ie. the only statement in the else block 'print("prime")' will get executed and the output will be 'prime'.