Python

 

Introduction

Prime numbers are numbers greater than 1 that have no divisors other than 1 and themselves. Generating prime numbers is a common problem in programming and mathematics. In this tutorial, we will write a Python program that generates all prime numbers up to a given limit. This exercise will help you understand how prime numbers work and how to implement an algorithm for prime number generation.

Objective

The objective of this program is to generate a list of prime numbers up to a specified limit. The program will take an integer as input and output all prime numbers less than or equal to that limit.

Python Code: Prime Number Generator


def is_prime(n):
    """Check if a number is prime."""
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

def generate_primes(limit):
    """Generate a list of prime numbers up to the given limit."""
    prime_numbers = []
    for number in range(2, limit + 1):
        if is_prime(number):
            prime_numbers.append(number)
    return prime_numbers

# Get user input and display prime numbers
limit = int(input("Enter the limit up to which you want prime numbers: "))
primes = generate_primes(limit)
print(f"Prime numbers up to {limit}: {primes}")
        

Explanation of the Program Structure

The program consists of two main functions:

  • is_prime(n): This function checks if a given number is prime. It returns True if the number is prime and False otherwise. The function iterates from 2 to the square root of the number to check for divisibility.
  • generate_primes(limit): This function generates a list of prime numbers up to the given limit. It calls the is_prime function for each number from 2 to the specified limit, adding the prime numbers to the prime_numbers list.

How to Run the Program

  1. Ensure you have Python installed on your system (Python 3.x is recommended).
  2. Open a text editor and paste the Python code into a new file, e.g., prime_generator.py.
  3. Open a terminal or command prompt and navigate to the folder containing the Python file.
  4. Run the program by typing python prime_generator.py and press Enter.
  5. The program will prompt you to enter a number, which is the limit up to which prime numbers will be generated. Once you input the number, it will display the list of prime numbers up to that limit.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

21 thoughts on “Prime Number Generator: Generate Prime Numbers List in Python”
  1. Does your site have a contact page? I’m having a tough time locating it but, I’d
    like to send you an e-mail. I’ve got some suggestions for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it develop over time.

  2. I have been exploring for a bit for any high-quality articles
    or weblog posts in this sort of space . Exploring in Yahoo I ultimately stumbled
    upon this web site. Studying this info So i am glad to show that I have a very just right uncanny feeling I discovered
    exactly what I needed. I such a lot without a
    doubt will make sure to don?t forget this site and give it a look regularly.

  3. I got this web site from my friend who shared with me on the topic
    of this web page and at the moment this time I am visiting this site and reading very informative articles
    or reviews here.

  4. I’m truly enjoying the design and layout of your site. It’s a very
    easy on the eyes which makes it much more enjoyable for me to come here and visit more often.
    Did you hire out a designer to create your theme?
    Excellent work!

  5. hey there and thank you for your info – I’ve certainly
    picked up anything new from right here. I did however
    expertise some technical points using this web site, as I experienced to
    reload the site many times previous to I could
    get it to load properly. I had been wondering if your web hosting is OK?

    Not that I am complaining, but slow loading instances times will sometimes affect your placement in google and could damage your high-quality score if advertising and marketing with Adwords.
    Anyway I’m adding this RSS to my e-mail and can look
    out for a lot more of your respective fascinating content.
    Ensure that you update this again very soon.

  6. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get
    four e-mails with the same comment. Is there any way you can remove
    people from that service? Appreciate it!

  7. Howdy! This is my first visit to your blog! We are a collection of volunteers
    and starting a new project in a community in the same niche.

    Your blog provided us beneficial information to work on. You
    have done a extraordinary job!

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)