Reading Time: 1 minutes
Prime Number or Not in Python: A Python program to check whether the provided number is prime or not.
Prime Number or Not in Python
# PRIME NUMBER OR NOT
# The program should contain two functions: one for concluding whether a number is prime or not; the other a main program that reads a number and displays the conclusion.
# A prime number is an integer greater than 1 that is only divisible by one and itself e.g. 2, 3, 5, 7, 11, 3, 17, 19 etc.
# INPUT: any number
# OUTPUT: conclusion if the given number is prime or not
# Also, ensure that your file when imported into another file doesn't trigger your program automatically
# THOUGHT PROCESS: Construct a function that takes an argument, and returns True or False based on logic ->
# Declare a main function(call it anything you want) and have the user input a number ->
# Pass the number to the declared function ->
# Based on the value returned by the function, display the conclusion ->
# include the bit to run the main function if the file has not been imported i.e. it has been run directly.
# function to determine whether the supplied number is prime or not.
def isPrime(num):
if num <= 1:
return False
for factor in range(2, num): # if there exists any number greater than 2 that
if num % factor == 0: # divides the given number evenly(with remainder 0, that is),
return False # then the given number is not prime
return True
# the main function
def mainProgram():
number = int(input("Enter the number to be tested: "))
if isPrime(number):
print(number, "is prime.")
else:
print(number, "is not prime.")
# if the file has been run directly AND not been imported, call the main function
if __name__ == "__main__":
mainProgram()
Try it here. You will have to call mainProgram() function yourself, since we have made the script independent by adding the if construct.







