This post shows how to check whether a number is prime in Python. A prime number is a number greater than 1 that has no divisors other than 1 and itself.

Check prime number in Python:

Explanation:
We check if the number is divisible by any integer from 2 up to its square root. If none divide it evenly, the number is prime. Checking only up to the square root keeps the program efficient.


Python Program to Check Prime Number

num = int(input("Enter a number: "))
is_prime = True

if num < 2:
    is_prime = False
else:
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            is_prime = False
            break

if is_prime:
    print(f"{num} is a prime number")
else:
    print(f"{num} is not a prime number")

Output:

Enter a number: 29
29 is a prime number

Practice more number-based logic problems daily with our Problem of the Day.