Python Program to Check Prime Number

1. Introduction

In this tutorial, we will write a Python program to determine if a given number is prime with simple arithmetic operations.

A prime number is a number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, as they cannot be divided evenly by any other numbers except for 1 and the number itself.

2. Program Steps

1. Define a number to check for primality.

2. Check if the number is less than 2, in which case it's not prime.

3. Iterate from 2 to the square root of the number.

4. If the number is divisible by any of these, it's not prime.

5. If none divide the number evenly, it is prime.

3. Code Program

# Function to check if a number is prime
def is_prime(number):
    # A number less than 2 is not prime
    if number < 2:
        return False
    # Check for factors from 2 up to the square root of the number
    for i in range(2, int(number ** 0.5) + 1):
        if number % i == 0:
            return False
    return True

# Number to be checked
num_to_check = 29
# Check if the number is prime and print the result
if is_prime(num_to_check):
    print(f"{num_to_check} is a prime number.")
    print(f"{num_to_check} is not a prime number.")


29 is a prime number.


1. The function is_prime takes an integer number as its input and returns a Boolean value indicating whether the number is prime.

2. If number is less than 2, the function returns False since 0, 1, and negative numbers are not prime.

3. The function then iterates through a loop from 2 to the square root of number (int(number 0.5) + 1) because a factor larger than the square root would have a complementary factor smaller than the square root.

4. Inside the loop, if number is divisible by i (number % i == 0), the function returns False as this means number is not prime.

5. If the loop completes without finding any factors, the function returns True, indicating that number is prime.

6. num_to_check is set to 29, and is_prime is called to check its primality.

7. The result is printed, indicating that 29 is indeed a prime number.