Home »
Python »
Python programs
Python program to check prime number
Python | Check prime or not: Here, we are going to learn how to check whether a given number is a prime number or not in Python programming language?
Submitted by IncludeHelp, on April 27, 2020
What is a prime number?
A prime number is a natural number that is greater than 1 and cannot be formed by multiplying two smaller natural numbers.
Given a number num, we have to check whether num is a prime number or not.
Example:
Input:
num = 59
Output:
59 is a prime number
Input:
num = 123
Output:
123 is not a prime number
Program to check prime number in Python
# Python program to check prime number
# Function to check prime number
def isPrime(n):
return all([(n % j) for j in range(2, int(n/2)+1)]) and n>1
# Main code
num = 59
if isPrime(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
num = 7
if isPrime(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
num = 123
if isPrime(num):
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Output
59 is a prime number
7 is a prime number
123 is not a prime number
Python Basic Programs »