Python Program To Check Prime Number Using Function

Chapter: Python Last Updated: 27-04-2023 16:44:27 UTC

Program:

            /* ............... START ............... */
                
def is_prime(n):
    """
    Returns True if n is a prime number, False otherwise.
    """
    if n < 2:  # 0 and 1 are not primes
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

# example usage
num = 17
if is_prime(num):
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")

                /* ............... END ............... */
        

Output

17 is a prime number
If you change the value of num to a non-prime number, like 20, then the program will output "20 is not 
a prime number".

Notes:

  • This program checks whether a given number is a prime number or not.The program defines a function called is_prime, which takes an integer n as input. The function checks whether n is less than 2, because any number less than 2 cannot be a prime number. If n is less than 2, the function returns False indicating that it is not a prime number.
  • If n is greater than or equal to 2, then the function proceeds to check if n is divisible by any number from 2 to the square root of n. If n is divisible by any number between 2 and the square root of n, then n is not a prime number, and the function returns False.
  • If the loop completes without finding any factors, then the number n is a prime number, and the function returns True.
  • Finally, the program uses the is_prime function to check whether a number is prime or not, and prints the appropriate message.

Tags

Python Program To Check Prime Number Using Function #Python find prime numbers in range #Prime number in Python

Similar Programs Chapter Last Updated
Python Program To Replace Characters In A String Python 03-06-2023
Python Program To Replace Blank Space With Hyphen Python 30-05-2023
Python Regex Match End Of String Python 30-05-2023
Python Check If String Contains Certain Characters Python 30-05-2023
How To Get The Length Of An Array In Python Python 27-05-2023
How To Find The First Duplicate Element In A Given Array Of Integers In Python Python 27-05-2023
How To Get Key And Value From Dictionary In Python Python 24-05-2023
Merge Two Dictionaries In Python Python 24-05-2023
Convert Two Lists Into A Dictionary In Python Python 24-05-2023
How To Remove An Element From A Set In Python Python 19-05-2023
Return A New Set Of Identical Items From Two Sets In Python Python 19-05-2023
Add A List Of Elements To A Set In Python Python 19-05-2023
Check If Two Lists Have At-least One Element Common In Python Python 17-05-2023
Python Program To Remove All The Occurrences Of An Element From A List Python 17-05-2023
Write A Python Program To Turn Every Item Of A List Into Its Square Python 15-05-2023
Write A Python Program To Concatenate Two Lists Index-wise Python 15-05-2023
Write A Python Program To Reverse A List Python 15-05-2023
How To Run Python Program In Terminal Python 14-05-2023
Python Date Format AM PM Python 14-05-2023
Python Add Months To Date Example Python 14-05-2023
Python Add Days To Date Example Python 14-05-2023
Python List All Elements In List Python 30-04-2023
Python Program To Sum All The Items In A List Python 27-04-2023
Python Interest Calculator Python 27-04-2023
Python Program To Print Calendar Of Any Year Python 26-04-2023
Python Program To Print Calendar Python 26-04-2023
How To Create XML File In Python Python 23-04-2023
Python Run Function At Specific Time Everyday Python 22-04-2023
Python Program For String Reverse Python 20-04-2023
Python Get Current Timestamp Python 20-04-2023

1 2 3