PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Programs and Examples » Python Programs to Find Prime Numbers within a Range

Python Programs to Find Prime Numbers within a Range

Updated on: March 31, 2025 | Leave a Comment

A Prime Number is a number that can only be divided by itself and 1 without remainders (e.g., 2, 3, 5, 7, 11).

In this article, we discuss the simple methods in Python to find prime numbers within a range. Let’s look at each of the methods with examples.

Table of contents

  • 1. Using loop for Checking Divisibility
  • 2. Optimized Method (Check up to √n)

1. Using loop for Checking Divisibility

This simple Python method checks for prime numbers by iterating through all numbers in the range using for loop and range() function and checking if they are divisible by any number other than one and themselves.

Code Example

def is_prime(num):
    if num <= 1:
        return False
    for i in range(2, num):
        if num % i == 0:
            return False
    return True

start = 50
end = 100
primes = []
for num in range(start, end + 1):
    if is_prime(num):
        primes.append(num)
print(primes)Code language: Python (python)

Output:

[53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Explanation

  • We use a for loop to iterate each number from start to end and check whether it is prime by using is_prime() function.
  • In is_prime() function, we loop from 2 to num and check if num is divisible by any number other than one and itself.
    • Using the modulo operator, we can find the divisor of the given num.
    • num % i returns the remainder when num is divided by i.
    • num % i == 0 checks if the remainder is zero, meaning num is perfectly divisible by i. Hence returns False as it’s not a prime number otherwise, returns True.
  • If the number is prime, we add it to the list primes.
  • This method is inefficient for larger numbers because of unnecessary modulo operation till num.

2. Optimized Method (Check up to √n)

In this Python method, instead of checking divisibility up to the number itself, we only need to check up to the square root of the number. This optimization reduces the number of unnecessary checks.

The math.sqrt() function in Python is used to calculate the square root of a given number.

For instance, consider n = 49:

  • The square root of 49 is 7.
  • The loop iterates only through numbers 2 to 7, skipping checks for 8 through 48.
  • When it finds 49 % 7 == 0, confirming that 49 is not a prime number.

It is more efficient than the above basic method.

Code Example

import math

def is_prime_optimized(num):
    if num <= 1:
        return False
    for i in range(2, int(math.sqrt(num)) + 1):
        if num % i == 0:
            return False
    return True

start = 50
end = 100
primes = []
for num in range(start, end + 1):
    if is_prime_optimized(num):
        primes.append(num)
print(primes)Code language: Python (python)

Output:

[53, 59, 61, 67, 71, 73, 79, 83, 89, 97]Code language: Python (python)

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com