I'm new to python. I just tried to write a basic program (to check whether a number is prime or not). I referred the code from an online site and tried to write one myself.
The code is as follows:
#PYTHON PROGRAM TO CHECK PRIME NUMBER
print("Enter a number :: ", end = " ")
num = int(input())
if num > 1:
for i in range(2,int(num/2)):
if(num%i) == 0:
print(str(num) + " is not prime")
break;
else:
print(str(num) + " is prime")
else:
print(str(num) + " is neither prime nor composite.")
The doubt I have is in the working of the code. It runs without any errors and produces the correct output as well.
The confusing code snippet is:
for i in range(2,int(num/2)):
if(num%i) == 0:
print(str(num) + " is not prime")
break;
else:
print(str(num) + " is prime")
EDIT: Why is there an else after the for loop and not after the if statement? Could someone please explain?