Back to Python
2026-03-015 min read

Check prime number (Python Programming)

Learn Check prime number (Python Programming) step by step with clear examples and exercises.

Why This Matters

Prime numbers play a crucial role in mathematics and computer science, particularly in cryptography and number theory. They are essential for secure communication over the internet, password generation, and more. Understanding how to check for prime numbers in Python is an important skill that every programmer should have.

Prerequisites

Before diving into this lesson, you should be familiar with the following Python programming concepts:

  • Python variables and data types
  • Basic arithmetic operations
  • Control structures (if...else, for loop)
  • Functions

Core Concept

A prime number is a positive integer greater than 1 that can only be divided evenly by 1 and itself. For example, the first few prime numbers are 2, 3, 5, 7, 11, and so on. In Python, we can write a program to check if a given number is prime or not using a simple loop and some conditional statements.

Here's an outline of the steps involved:

  1. Define a function that takes an integer as input.
  2. Initialize a flag variable to False by default, indicating that the number is not prime.
  3. Check if the input number is less than or equal to 1. If it is, return that the number is not prime since only numbers greater than 1 can be prime.
  4. Begin a loop starting from 2 (the first possible divisor) and go up to the square root of the input number. This is because a larger factor of the number would have already been found in the smaller factors.
  5. In each iteration of the loop, divide the input number by the current divisor. If the division results in no remainder (i.e., the quotient is an integer), set the flag variable to True and break out of the loop since we've found a factor other than 1.
  6. If the loop completes without finding any factors, set the flag variable to True and return that the number is prime.

Here's an example implementation:

def check_prime(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True

Worked Example

Let's test the check_prime() function with some examples:

print(check_prime(2)) # Output: True
print(check_prime(3)) # Output: True
print(check_prime(4)) # Output: False
print(check_prime(5)) # Output: True
print(check_prime(6)) # Output: False
print(check_prime(7)) # Output: True
print(check_prime(8)) # Output: False

Common Mistakes

  1. Not checking for the base case (num <= 1): If you forget to check for this case, your function will incorrectly classify all numbers less than or equal to 1 as prime.
  2. Stopping the loop too early: If you stop the loop at int(num ** 0.5), you may miss larger factors of the number that are not multiples of any smaller factor found in the loop. This can cause your function to incorrectly classify composite numbers as prime.**
  3. Incorrectly handling even numbers: If an even number other than 2 is passed to the function, it should be checked for divisibility by 2 before checking other factors. If you forget this step, the function may incorrectly classify even numbers greater than 2 as prime.
  4. ### Optimization
  • Using bitwise operations: Instead of iterating through all possible divisors, we can use bitwise operations to check if a number is prime more efficiently. This approach works because only odd numbers can be prime, and the binary representation of an odd composite number will contain a run of consecutive set bits.
  • Using the Sieve of Eratosthenes: The Sieve of Eratosthenes is an algorithm for finding all prime numbers up to a specified limit. It works by iteratively marking the multiples of each prime number as composite, leaving the unmarked numbers as primes. This algorithm can be used to create a table of all prime numbers up to a given limit, which can then be used to check if any arbitrary number is prime.

Practice Questions

  1. Write a Python program that prints all prime numbers between 1 and 50.
  2. Modify the check_prime() function to return the smallest factor of the number if it is composite, instead of just returning False.
  3. Implement an optimized version of the check_prime() function using bitwise operations or the Sieve of Eratosthenes algorithm.
  4. ### Advanced Topics
  • Miller-Rabin primality test: The Miller-Rabin primality test is a probabilistic algorithm for determining whether a number is prime or composite. It is faster than the simple loop approach presented in this lesson and can be used to check large numbers for primality with high probability.
  • Elliptic curve cryptography: Elliptic curve cryptography (ECC) is a public-key cryptography technique that uses elliptic curves over finite fields. ECC provides the same level of security as RSA using smaller key sizes, making it more efficient for certain applications. Prime numbers play an essential role in the construction of elliptic curves used in ECC.

FAQ

  1. Why do we stop at the square root of the input number in the loop?: Since a larger factor of the number would have already been found in the smaller factors, checking divisors up to the square root of the number is sufficient to determine if the number is prime or not.
  2. Can we check for primes more efficiently than iterating through all numbers up to the square root?: Yes, there are more efficient algorithms for prime number testing such as the Sieve of Eratosthenes and Miller-Rabin primality test. However, the simple loop approach presented in this lesson is easy to understand and sufficient for most programming tasks.
  3. Is it necessary to check for divisibility by 2 separately for even numbers?: Yes, checking for divisibility by 2 first can help reduce the number of iterations needed in the main loop since an even number greater than 2 will always be composite. This optimization is especially useful when dealing with large numbers.
  4. ### Optimization
  • Using bitwise operations: Instead of iterating through all possible divisors, we can use bitwise operations to check if a number is prime more efficiently. This approach works because only odd numbers can be prime, and the binary representation of an odd composite number will contain a run of consecutive set bits.
  • Using the Sieve of Eratosthenes: The Sieve of Eratosthenes is an algorithm for finding all prime numbers up to a specified limit. It works by iteratively marking the multiples of each prime number as composite, leaving the unmarked numbers as primes. This algorithm can be used to create a table of all prime numbers up to a given limit, which can then be used to check if any arbitrary number is prime.
Check prime number (Python Programming) | Python | XQA Learn